【发布时间】:2017-03-24 16:23:12
【问题描述】:
我想在同一个表之间创建多个关系
Project <--> ProjectManagers <--> Employees
Project <--> ProjectEmployees <--> Employees
我已经有一个项目和员工实体。 如何与注解建立联系?
已经尝试过多对多双向关系。但是学说会生成 1 个连接表,但也需要一个 projects_managers 表。
<?php
/** @Entity */
class Project
{
// ...
/**
* Many Projects have Many Employees.
* @ManyToMany(targetEntity="Employee", inversedBy="projects")
* @JoinTable(name="projects_employees")
*/
private $employees;
public function __construct() {
$this->employees = new \Doctrine\Common\Collections\ArrayCollection();
}
// ...
}
/** @Entity */
class Employee
{
// ...
/**
* Many Employees have Many Projects.
* @ManyToMany(targetEntity="Project", mappedBy="employees")
*/
private $projects;
public function __construct() {
$this->projects = new \Doctrine\Common\Collections\ArrayCollection();
}
// ...
}
【问题讨论】:
-
您能否在 Employee 中添加一个字段来指定他们是 ProjectManager 还是 ProjectEmployee?假设他们不能同时是...
-
您需要创建 ProjectManagers 和 ProjectEmployees 实体,然后根据需要建立一对多关系。
标签: php mysql symfony doctrine-orm annotations