我正在嘗試連接兩個表。我的用戶表和我的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%肯定在這一點上...任何人都可以指向我的一些教程或工作示例爲此?
乾杯
的可能重複[ZF2 - 學說ORM,簡單表格加入(http://stackoverflow.com/questions/23468267/zf2-doctrine-orm-simple - 表聯接) – Wilt