【发布时间】:2016-11-05 11:02:28
【问题描述】:
我是 Symfony Doctrine 的新手,在加入实体方面需要一些帮助。
通常列是按主键 ID 连接
/**
* User
*
* @ORM\Table(name="users")
* @ORM\Entity(repositoryClass="MainBundle\Repository\UserRepository")
* UniqueEntity("email", message="Account with email already exists.")
*/
class User implements AdvancedUserInterface, \Serializable
{
/**
* @var \MainBundle\Entity\PersonDetails
*
* @ORM\ManyToOne(targetEntity="MainBundle\Entity\Person")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="person_details_id", referencedColumnName="id", nullable=true)
* })
*/
private $personDetails = null;
没关系。
但问题是我想通过用户实体中的 id 字段加入 Relation OneToOne 中的两列
/**
* User
*
* @ORM\Table(name="users")
* @ORM\Entity(repositoryClass="MainBundle\Repository\UserRepository")
* UniqueEntity("email", message="Account with email already exists.")
*/
class User implements AdvancedUserInterface, \Serializable
{
/**
* @var \MainBundle\Entity\PersonDetails
*
* @ORM\ManyToOne(targetEntity="MainBundle\Entity\Person")
* @ORM\JoinColumn(name="id", referencedColumnName="user_id", nullable=true)
* })
*/
private $personDetails = null;
当我尝试以这种方式加入列时出现错误
MainBundle\Entity\PersonDetails 上的主键 ID 缺失值
是否可以索引 id 以外的其他字段,或者我试图做的事情是不可能的?
谢谢大家。
【问题讨论】:
-
请用正确的缩进修正问题中的代码,它很难阅读。
User实体中的 id field 和 primary id 有什么区别?似乎缺少属性(即带有@ORM\Id()的属性)......回答您的问题需要这些属性。
标签: symfony doctrine-orm