Symfony2 实体外键
Symfony2 entity foreign keys
我想不通..
为什么我无法访问 Country
table?
countryName
应该显示 Great Britain
但它没有。
这是我的转储($User):
我的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;
你的 Country
对象现在只是一个 Proxy 对象 - dump
函数不调用 Doctrine 来获取相关对象。在转储之前尝试获取您的对象,例如:
dump($User->getCountry()):
dump($User);
或者在 QueryBuilder
中尝试向左加入 Country
或在 Doctrine2 中找到有关延迟加载的信息 here
取决于您获取用户的方式,也许您使用的是延迟加载,只有当您显式调用 getter 时才会获取国家/地区,始终与用户一起获取国家/地区尝试:
/**
*
* @ORM\ManyToOne(targetEntity="Dashboard\MainBundle\Entity\Country", cascade={"persist"}, fetch="EAGER")
* @ORM\JoinColumn(name="country_id", referencedColumnName="id", nullable=true)
*
*/
private $countryId;
但我们仍然需要知道您如何让用户延迟加载可能会覆盖急切的获取。
我想不通..
为什么我无法访问 Country
table?
countryName
应该显示 Great Britain
但它没有。
这是我的转储($User):
我的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;
你的 Country
对象现在只是一个 Proxy 对象 - dump
函数不调用 Doctrine 来获取相关对象。在转储之前尝试获取您的对象,例如:
dump($User->getCountry()):
dump($User);
或者在 QueryBuilder
中尝试向左加入Country
或在 Doctrine2 中找到有关延迟加载的信息 here
取决于您获取用户的方式,也许您使用的是延迟加载,只有当您显式调用 getter 时才会获取国家/地区,始终与用户一起获取国家/地区尝试:
/**
*
* @ORM\ManyToOne(targetEntity="Dashboard\MainBundle\Entity\Country", cascade={"persist"}, fetch="EAGER")
* @ORM\JoinColumn(name="country_id", referencedColumnName="id", nullable=true)
*
*/
private $countryId;
但我们仍然需要知道您如何让用户延迟加载可能会覆盖急切的获取。