通过非主键 - id 的引用列名称连接列

Ste*_*sic 6 symfony doctrine-orm

我是 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;
Run Code Online (Sandbox Code Playgroud)

还行吧。

但问题是我想通过用户实体中的 id 字段连接关系 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;
Run Code Online (Sandbox Code Playgroud)

当我尝试以这种方式加入列时,出现错误

MainBundle\Entity\PersonDetails 上的主键 ID 缺少值

是否可以索引 id 以外的其他字段,或者我尝试做的事情是不可能的?

多谢你们。

Nic*_*ich 2

您混淆了声明中应引用的列名称和字段名称@JoinColumn

@JoinColumn(name="id", referencedColumnName="user_id")
Run Code Online (Sandbox Code Playgroud)

user_id通过这种方式,Doctrine 会查找以您的实体命名的字段/属性User。我猜您希望连接表中的列被命名user_id并且条目是id实体的User

用户详细信息

/**
 * @ORM\Entity
 */
class UserDetail
{
    /**
     * @ORM\ManyToOne(
     *   targetEntity="User",
     *   inversedBy="details"
     * )
     * @ORM\JoinColumn(
     *   name="user_id",
     *   referencedColumnName="id"
     * )
     */
    protected $user;

    public function setUser(User $user)
    { 
        $this->user = $user;

        return $this;
    }

    /** @ORM\Column() */
    protected $key;

    /** @ORM\Column() */
    protected $value;

    public function __construct($key, $value)
    {
       $this->key = $key;
       $this->value = $value;
    }
Run Code Online (Sandbox Code Playgroud)

用户

class User
{
    /**
     * @ORM\Id()
     * @ORM\Column(type="integer")
     */
    protected $id;

    /** 
     * @ORM\OneToMany(
     *   targetEntity="UserDetail",
     *   mappedBy="user", 
     *   cascade={
     *     "persist",
     *     "remove",
     *     "merge"
     *   },
     *   orphanRemoval=true
     * )
     */
    protected $details;

    public function __construct()
    {
        $this->details = new ArrayCollection();
    }

    public function addDetail(UserDetail $detail)
    {
        $detail->setUser($this);
        $this->details->add($detail);

        return $this;
    }
Run Code Online (Sandbox Code Playgroud)

现在,如果您User像这样添加详细信息,然后保留/刷新:

$user->addDetail(new UserDetail('Height', '173cm'));
Run Code Online (Sandbox Code Playgroud)

这将导致表中的连接列user_detail如下所示:

| key           | value     | user_id |
|---------------|-----------|---------|
| Height        | 173cm     | 1       |
Run Code Online (Sandbox Code Playgroud)