我关注有关身份验证的文档。现在我想要实现角色
这是我的实体
user.php的
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Role", inversedBy="users", cascade={"all"})
* @ORM\JoinTable(name="user_role",
* joinColumns={
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="role_id", referencedColumnName="id")
* }
* )
*/
private $roles;
public function __construct () {
$this->roles = new ArrayCollection();
}
public function getRoles () {
return $this->roles->toArray();
}
public function getRoles2 () {
return $this->roles;
}
比我有三张桌子
用户user_role角色
角色
<?php
namespace ADV\UserBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Role
*
* @ORM\Table(name="role")
* @ORM\Entity(repositoryClass="ADV\UserBundle\Entity\RoleRepository")
*/
class Role
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=30)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="role", type="string", length=20)
*/
private $role;
/**
* @ORM\ManyToMany(targetEntity="User", mappedBy="roles")
*/
private $users;
public function __construct()
{
$this->users = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Role
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set role
*
* @param string $role
* @return Role
*/
public function setRole($role)
{
$this->role = $role;
return $this;
}
/**
* Get role
*
* @return string
*/
public function getRole()
{
return $this->role;
}
/**
* Add users
*
* @param \ADV\UserBundle\Entity\User $users
* @return Role
*/
public function addUser(\ADV\UserBundle\Entity\User $users)
{
$this->users[] = $users;
return $this;
}
/**
* Remove users
*
* @param \ADV\UserBundle\Entity\User $users
*/
public function removeUser(\ADV\UserBundle\Entity\User $users)
{
$this->users->removeElement($users);
}
/**
* Get users
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getUsers()
{
return $this->users;
}
}
我创建了两个表单,userType和Roletype,以及UserType&#34; embed&#34; roleType
->add('roles', 'collection', array(
'type' => new RoleType()
))
我的控制器:
public function registrationAction (Request $request) {
$user = new User();
$role = new Role();
$user->getRoles2()->add($role);
$form = $this->createForm(new UserType(), $user);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getEm();
try {
$em->beginTransaction();
$factory = $this->get('security.encoder_factory');
$encoder = $factory->getEncoder($user);
$user->setSalt(md5(time()));
$password = $encoder->encodePassword($user->getPassword(), $user->getSalt());
$user->setPassword($password);
$em->persist($user);
$em->flush();
$em->commit();
} catch (\Exception $e) {
$em->rollback();
}
}
return array(
'form' => $form->createView()
);
}
我有正确的表格
通过这种关系找到了一个新的实体 &#39; ADV \ UserBundle \实体\用户#角色&#39;没有配置为级联 实体的持久操作:ROLE_WRITER。要解决这个问题: 在这个未知实体上显式调用EntityManager#persist() 或者配置级联在映射中保持此关联 例如@ManyToOne(..,cascade = {&#34; persist&#34;})。
但我想使用ManyToMay,如果我添加级联,每次创建新用户时,也要创建一个新的角色相关