MechEngineer
MechEngineer

Reputation: 1429

Doctrine2 criteria filtering can't find property

I'm filtering an entity in Symfony2 that has an association with a "start_date" property. This is the value in the DB and it has a Doctrine-generated getter of getStartDate. Inside the filter method, I use an expression of

Criteria::expr()->gt('start_date', $now)

but that fails with an error about accessing a protected property. If I change the criteria to

Criteria::expr()->gt('startDate', $now)

it fails again but this time with an error about not having that property on the object. Which is correct? I shouldn't need to add a separate getting for just the criteria filtering.

Upvotes: 4

Views: 1577

Answers (1)

Victor Bocharsky
Victor Bocharsky

Reputation: 12306

Correct is first case:

Criteria::expr()->gt('start_date', $now)

Is getter public? Try to make this property public. Is error still display in this case?

Also try to add next getters for test:

public function start_date(){};

public function getStart_date(){};

You must to correct your code to this:

/** 
 * @ORM\Column(type="datetime", name="start_date") 
 */

protected $startDate; 

public function getStartDate() {

    return $this->startDate;
}

and then use:

Criteria::expr()->gt('startDate', $now)

Symfony need to call your proprties in camelCase style, then camelCase getters will be work

Upvotes: 2

Related Questions