[英]Doctrine2 Relation with 3 entities
我有3個實體:用戶角色公司
通常,一個用戶具有一個角色,而許多公司。 我已經成功設置了。
但是,如果用戶具有取決於公司的角色,我將如何建立關系? 我嘗試了其他實體UserCompanyRole。
在用戶中:
/** @ORM\OneToMany(targetEntity="UserCompanyRole", mappedBy="user")*/
protected $userCompanyRoles;
public function getUserCompanyRoles() {
return $this->userCompanyRoles;
}
public function addUserCompanyRole(UserCompanyRole $userCompanyRole) {
$this->userCompanyRoles[] = $userCompanyRole;
}
角色和公司相同
在UserCompanyRole中:
/** @ORM\ManyToOne(targetEntity="User", inversedBy="userCompanyRoles")
* @ORM\JoinColumn( onDelete = "CASCADE" )
*/
private $user;
public function getUser() {
return $this->user;
}
public function setUser(User $user = null) {
$this->user = $user;
}
角色和公司功能相同。
創建查詢時,這將變得很復雜。 也許有更好的方法?
如果用戶可以同時擔任多個角色和公司,我會說是。 一個公司中的一個角色有多名員工。 所以我會讓它成為角色。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.