Symfony2学说实体未水合 [英] Symfony2 Doctrine entity not hydrated

查看:81
本文介绍了Symfony2学说实体未水合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从实体经理那里得到了一个实体成员,做了一个var_dump,除了与 Family ,所以我尝试了 var_dump($ member-&get; getFamily()); ,令人惊讶的是,唯一正确的值是该家庭的ID,所有其他属性均为null (数据库中不是这种情况……)

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...)

这是我的会员资料

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

和我的家庭实体的东西

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

所有获取器和设置器均由Doctrine生成。只是ID似乎被水合了,其余的就没有了。知道吗?

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

编辑:var_dump结果

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


推荐答案

Doctrine延迟加载数据,除非您将其加入您的查询(并将其别名添加到select()调用)。这意味着您将获得示例中的代理,而不是真正的家庭实体。

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.

在您调用非 getId 之前,该代理只有一个可以访问的字段。对象上的函数(如 getNom())将保留为代理,但在该调用时,教义会执行查询以完全加载它。即使您将家庭代理人传递给树枝,也会发生这种情况。

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.

这篇关于Symfony2学说实体未水合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆