Symfony2 Doctrine entity not hydrated

梦想的初衷 提交于 2019-12-12 12:11:33

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/27355584/symfony2-doctrine-entity-not-hydrated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!