简体   繁体   English

Symfony 3:一个实体中的许多关联

[英]Symfony 3: Many associations in one entity

I have 2 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;
//....
}

When im trying generate relations in database I got nothing. 当我试图在数据库中生成关系时,我什么也没得到。 But with only one one association it is working. 但是,只有一个协会,它正在运作。 I'm using console command: 我正在使用控制台命令:

doctrine:schema:update --force 

Try with different names for join columns. 为连接列尝试使用不同的名称。

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 http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/association-mapping.html#association-mapping-defaults

Based on your comment to Akash what you want isnt really possible how you have laid it out without some work. 根据您对Akash的评论,您真正想要的是没有任何工作就如何布置的。 The easiest way to accomplish it is with an extra column on your other 2 tables. 最简单的方法是在其他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;

    //...
}

For reference take a look at the docs for more info on bidirectional associations 供参考,请参阅文档以获取有关双向关联的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM