簡體   English   中英

Symfony 3:一個實體中的許多關聯

[英]Symfony 3: Many associations in one entity

我在一個實體中有2個關聯

class User
{
/**
 * @ORM\OneToOne(targetEntity="Employee")
 * @ORM\JoinColumn(name="id", referencedColumnName="id")
 */
 protected $employee;


/**
 * @ORM\OneToOne(targetEntity="Client")
 * @ORM\JoinColumn(name="id", referencedColumnName="id")
 */
protected $client;
//....
}

當我試圖在數據庫中生成關系時,我什么也沒得到。 但是,只有一個協會,它正在運作。 我正在使用控制台命令:

doctrine:schema:update --force 

為連接列嘗試使用不同的名稱。

class User
{

    /**
     * @ORM\OneToOne(targetEntity="Employee")
     * @ORM\JoinColumn(name="employee_id", referencedColumnName="id")
     */
    protected $employee;

    /**
     * @ORM\OneToOne(targetEntity="Client")
     * @ORM\JoinColumn(name="client_id", referencedColumnName="id")
     */
    protected $client;

    //...
}

http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/association-mapping.html#association-mapping-defaults

根據您對Akash的評論,您真正想要的是沒有任何工作就如何布置的。 最簡單的方法是在其他2個表上增加一列。

class User
{    

    /**
     * @ORM\OneToOne(targetEntity="Employee", mappedBy="user")
     */
    protected $employee;

    /**
     * @ORM\OneToOne(targetEntity="Client", mappedBy="user")
     */
    protected $client;

    //...
}

class Employee
{    

    /**
     * @ORM\OneToOne(targetEntity="User", inversedBy="employee")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    protected $user;

    //...
}

class Client
{    

    /**
     * @ORM\OneToOne(targetEntity="User", inversedBy="client")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    protected $user;

    //...
}

供參考,請參閱文檔以獲取有關雙向關聯的更多信息

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM