2014-05-03 30 views
0

我正在嘗試連接兩個表。我的用戶表和我的user_role_linker表。ZF2 - 使用ORM Doctrine一對一連接和集合

用戶表:USER_ID,姓名,電子郵件......等 user_role_linker表:USER_ID,ROLE_ID(這是從ZfcUser/BjyAithorize)

我列出我的用戶視圖,我想包括其在視圖中的作用。幸運的是,user_role_linker表使用實際的角色名稱作爲它的ID,所以我只需要進行一次連接。

我被告知要實現這個我需要使用「集合」。我已閱讀Doctrine Manual中有關收集的所有內容,並且已經放棄了一些代碼。然而,我有點不確定如何把它放在一起。這是我迄今:

<?php 
    namespace Administration\Entity; 
    use Doctrine\ORM\Mapping as ORM; 
    use Doctrine\ORM\MApping\OneToOne; 
    use Doctrine\Common\Collections\ArrayCollection; 

    /** @ORM\Entity */ 

    class User { 
     /** 
     * @ORM\Id 
     * @ORM\GeneratedValue(strategy="AUTO") 
     * @ORM\Column(type="integer",name="user_id") 
     * @OneToOne(targetEntity="UserRole", mappedBy="user_id") 
     */ 
     protected $user_id; 

     /** @ORM\Column(type="integer", name="parent_id") */ 
     protected $parent_id; 

     /** @ORM\Column(type="string", name="title") */ 
     protected $title; 

     /** @ORM\Column(type="string", name="name") */ 
     protected $name; 


     //etc. 

     //Setters and getters 

     public function getUserId() { 
      return $this->user_id; 
     } 


     public function setTitle($title) { 
      $this->title = $title; 
     } 

     public function setName($name) { 
      $this->name = $name; 
     } 


     //etc. 

     //Constructor to setup the collection 

     /** @OneToOne(targetEntity="UserRole", mappedBy="user_id") **/ 
     private $user_role; 

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

     public function getUserRole() 
     { 
      return $this->user_role; 
     } 

    } 

我UserRole的實體看起來是這樣的:

<?php 
namespace Administration\Entity; 
use Doctrine\ORM\Mapping as ORM; 
use Doctrine\ORM\MApping\OneToOne; 
use Doctrine\Common\Collections\ArrayCollection; 

/** @ORM\Entity */ 
class UserRole { 
    /** 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    * @ORM\Column(type="integer",name="user_id") 
    * @OneToOne(targetEntity="User", mappedBy="user_id")) 
    */ 
    protected $user_id; 

    /** @ORM\Column(type="string", name="role_id") */ 
    protected $role_id; 

    public function getRoleId() { 
     return $this->role_id; 
    } 


} 

抓住用戶我有一個名爲形式的控制器功能,我懷疑我應該設置了收集在這一點上...

public function getUsers() 
    { 
     return $this->em->getRepository('Administration\Entity\User')->findAll(); 
    } 

按照文檔我shouls做這樣的事情:

$user_group = new UserRole(); 
$user = new User(); 
$user->getUserRole()->add($user_group); 

我不是100%肯定在這一點上...任何人都可以指向我的一些教程或工作示例爲此?

乾杯

+0

的可能重複[ZF2 - 學說ORM,簡單表格加入(http://stackoverflow.com/questions/23468267/zf2-doctrine-orm-simple - 表聯接) – Wilt

回答

0

我想你想要的是多對多的關聯關係:一個用戶可以擁有多個角色,一個角色可以分配給多個用戶。 Doctrine將爲您創建連接表。

假設你有Role實體:

用戶實體:

/** @ORM\Entity */ 
class User { 
    /** 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    * @ORM\Column(type="integer",name="user_id") 
    */ 
    protected $user_id; 

    /** @ORM\Column(type="integer", name="parent_id") */ 
    protected $parent_id; 

    /** @ORM\Column(type="string", name="title") */ 
    protected $title; 

    /** @ORM\Column(type="string", name="name") */ 
    protected $name; 

    // roles association: 

    /** @ORM\ManyToMany(targetEntity="Role") 
    protected $roles; 

    // getters & setters 

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

} 

你不需要UserRole表。學說將創建users_roles表,鏈接UserRole實體。

然後添加角色到用戶:

$user = new User(); 

// $role1 and $role2 are instances of Role entity 
$user->getRoles()->add($role1); 
$user->getRoles()->add($role2);