ZF2-使用ORM原则处理一对一连接和集合
我正在尝试连接两个表。我的用户表和我的用户角色链接器表 用户表:用户id、名称、电子邮件。。。等 user_role_linker表:user_id,role_id(来自ZfcUser/BjyAithorize) 我将我的用户列在一个视图中,我想在视图中包含他们的角色。幸运的是,user_role_linker表使用实际的角色名称作为其ID,因此我只需进行一次连接 我被告知要实现这一点,我需要使用“集合”。我已经阅读了中所有关于集合的内容,并且写下了一些代码。然而,我有点不确定如何把这一切放在一起。这就是我迄今为止所做的:ZF2-使用ORM原则处理一对一连接和集合,orm,doctrine-orm,zend-framework2,Orm,Doctrine Orm,Zend Framework2,我正在尝试连接两个表。我的用户表和我的用户角色链接器表 用户表:用户id、名称、电子邮件。。。等 user_role_linker表:user_id,role_id(来自ZfcUser/BjyAithorize) 我将我的用户列在一个视图中,我想在视图中包含他们的角色。幸运的是,user_role_linker表使用实际的角色名称作为其ID,因此我只需进行一次连接 我被告知要实现这一点,我需要使用“集合”。我已经阅读了中所有关于集合的内容,并且写下了一些代码。然而,我有点不确定如何把这一切放在一
<?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;
}
}
根据文件,我应该这样做:
$user_group = new UserRole();
$user = new User();
$user->getUserRole()->add($user_group);
在这一点上我不是100%确定。。。有人能给我指一些关于这方面的教程或工作示例吗
干杯我认为您需要的是多对多关联:一个用户可以有多个角色,一个角色可以分配给多个用户。条令将为您创建联接表 假设您拥有
角色
实体:
用户实体:
/** @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
表。条令将创建链接用户
和角色
实体的用户
角色表
然后将角色添加到用户:
$user = new User();
// $role1 and $role2 are instances of Role entity
$user->getRoles()->add($role1);
$user->getRoles()->add($role2);
可能重复的
/** @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();
}
}
$user = new User();
// $role1 and $role2 are instances of Role entity
$user->getRoles()->add($role1);
$user->getRoles()->add($role2);