Symfony2实体外键

时间:2015-06-03 21:37:52

标签: symfony doctrine-orm foreign-keys entity

我无法理解......

为什么我无法访问Country表?

countryName应该显示Great Britain但不会显示。

这是我的转储($ User):

enter image description here

我的User实体代码:

 /**
 *
 * @ORM\ManyToOne(targetEntity="Dashboard\MainBundle\Entity\Country", cascade={"persist"})
 * @ORM\JoinColumn(name="country_id", referencedColumnName="id", nullable=true)
 *
 */
private $countryId;

我的Country实体代码:

/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

2 个答案:

答案 0 :(得分:1)

您的Country对象现在只是一个代理对象 - dump函数不会调用Doctrine来获取相关对象。在dump之前尝试获取对象,例如:

dump($User->getCountry()):
dump($User);

或尝试在QueryBuilder中加入Country

或者在Doctrine2 here

中找到有关延迟加载的信息

答案 1 :(得分:1)

根据您获得用户的方式,可能是您正在使用的延迟加载,只有在您明确调用getter时才能获得该国家/地区,以便始终通过用户尝试获取国家/地区:

 /**
 *
 * @ORM\ManyToOne(targetEntity="Dashboard\MainBundle\Entity\Country", cascade={"persist"}, fetch="EAGER")
 * @ORM\JoinColumn(name="country_id", referencedColumnName="id", nullable=true)
 *
 */
private $countryId;

但是我们仍然需要知道你是如何让用户延迟加载可能会覆盖fetch eager。

相关问题