ovesco
ovesco

Reputation: 633

Symfony2 Doctrine entity not hydrated

I get an entity Member from the entity manager, did a var_dump, everything ok except for the manyToOne relation with Family, so I tried a var_dump($member->getFamily()); and surprisingly, the only correct value was the family's ID, all the other properties were null (which is not the case in the database...)

Here is my Member stuff

/**
 * @var Family
 *
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Family", inversedBy="members")
 * @ORM\JoinColumn(name="family_id", referencedColumnName="id")
 */
private $family;

And my Family entity stuff

/**
 * @ORM\OneToMany(targetEntity="AppBundle\Entity\Member", mappedBy="family", cascade={"persist", "remove"})
 */
private $members;

All getters and setters are generated by Doctrine. It's just that only the id seems to be hydrated, not the rest. Any idea ?

EDIT : the var_dump result

private 'family' => 
object(Proxies\__CG__\AppBundle\Entity\Family)[427]
  public '__initializer__' => 
    object(Closure)[405]
  public '__cloner__' => 
    object(Closure)[406]
  public '__isInitialized__' => boolean false
  private 'id' (AppBundle\Entity\Family) => int 1
  private 'members' (AppBundle\Entity\Family) => null
  private 'adress' (AppBundle\Entity\Family) => null
  protected 'telephone' => null
  protected 'email' => null
  private 'nom' (AppBundle\Entity\Family) => null
  private 'isValid' (AppBundle\Entity\Family) => null

Upvotes: 4

Views: 4614

Answers (1)

Maerlyn
Maerlyn

Reputation: 34107

Doctrine lazy-loads data unless you join it in your query (and add its alias to the select() call). This means you get a proxy like in your example, not a real Family entity. The proxy only has the one field it has access to, the family_id.

Until you call a non-getId function on the object (like getNom()) this stays a proxy, but at that call doctrine does the query to load it completely. This will happen even if you pass that family proxy to twig.

Upvotes: 5

Related Questions