我在使用两个具有多对多关系的对象实现symfony 3表单时遇到问题。
我有两个数据表,“用户”和“角色”。我想要一个表单,我可以在其中编辑用户并为用户分配一些角色。详细地说,我想要一个表单,其中每个Role都是一个复选框,我可以选择用户拥有的角色。 我知道,我必须实现一个新的UserType,但是如何实现动态复选框?
这是我的用户级:
<?php
// src/AppBundle/Entity/User.php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Table(name="app_users")
* @ORM\Entity(repositoryClass="AppBundle\Entity\UserRepository")
*/
class User implements UserInterface, \Serializable
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=25, unique=true)
*/
private $username;
/**
* @ORM\Column(type="string", length=64)
*/
private $password;
/**
* @ORM\Column(type="string", length=60, unique=true)
*/
private $email;
/**
* @ORM\Column(name="is_active", type="boolean")
*/
private $isActive;
/**
* @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
* @ORM\JoinTable(name="user_roles")
*/
private $roles;
public function __construct()
{
$this->isActive = true;
// may not be needed, see section on salt below
// $this->salt = md5(uniqid(null, true));
}
public function getUsername()
{
return $this->username;
}
public function getSalt()
{
// you *may* need a real salt depending on your encoder
// see section on salt below
return null;
}
public function getPassword()
{
return $this->password;
}
public function getRoles()
{
$permissionsArray = array();
foreach ($this->roles as $role)
{
$rolesPermissions = $role->getPermissions();
for($i=0;$i<count($rolesPermissions);$i++)
if(!in_array($rolesPermissions[$i],$permissionsArray))
$permissionsArray[] = $rolesPermissions[$i];
}
return $permissionsArray;
}
public function eraseCredentials()
{
}
/** @see \Serializable::serialize() */
public function serialize()
{
return serialize(array(
$this->id,
$this->username,
$this->password,
// see section on salt below
// $this->salt,
));
}
/** @see \Serializable::unserialize() */
public function unserialize($serialized)
{
list (
$this->id,
$this->username,
$this->password,
// see section on salt below
// $this->salt
) = unserialize($serialized);
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set username
*
* @param string $username
*
* @return User
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Set password
*
* @param string $password
*
* @return User
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Set email
*
* @param string $email
*
* @return User
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set isActive
*
* @param boolean $isActive
*
* @return User
*/
public function setIsActive($isActive)
{
$this->isActive = $isActive;
return $this;
}
/**
* Get isActive
*
* @return boolean
*/
public function getIsActive()
{
return $this->isActive;
}
}
这是我的角色类:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Role
*
* @ORM\Table(name="role")
* @ORM\Entity(repositoryClass="AppBundle\Repository\RoleRepository")
*/
class Role
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="bezeichnung", type="string", length=255, unique=true)
*/
private $bezeichnung;
/**
* @ORM\ManyToMany(targetEntity="User", mappedBy="roles")
*/
private $users;
/**
* @ORM\ManyToMany(targetEntity="Permission", inversedBy="roles")
* @ORM\JoinTable(name="roles_permissions")
*/
private $permissions;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set bezeichnung
*
* @param string $description
*
* @return Role
*/
public function setBezeichnung($bezeichnung)
{
$this->bezeichnung = $bezeichnung;
return $this;
}
/**
* Get bezeichnung
*
* @return string
*/
public function getBezeichnung()
{
return $this->bezeichnung;
}
/**
* Constructor
*/
public function __construct()
{
$this->users = new \Doctrine\Common\Collections\ArrayCollection();
$this->roles = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add user
*
* @param \AppBundle\Entity\User $user
*
* @return Role
*/
public function addUser(\AppBundle\Entity\User $user)
{
$this->users[] = $user;
return $this;
}
/**
* Remove user
*
* @param \AppBundle\Entity\User $user
*/
public function removeUser(\AppBundle\Entity\User $user)
{
$this->users->removeElement($user);
}
/**
* Get users
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getUsers()
{
return $this->users;
}
/**
* Add permission
*
* @param \AppBundle\Entity\Permission $permission
*
* @return Role
*/
public function addPermission(\AppBundle\Entity\Permission $permission)
{
$this->permissions[] = $permission;
return $this;
}
/**
* Remove permission
*
* @param \AppBundle\Entity\Permission $permission
*/
public function removePermission(\AppBundle\Entity\Permission $permission)
{
$this->permissions->removeElement($permission);
}
/**
* Get permissions
*
* @return Array
*/
public function getPermissions()
{
$permissionsArray = array();
foreach ($this->permissions as $permission)
$permissionsArray[] = "ROLE_".$permission->getTechBezeichnung();
return $permissionsArray;
}
}
答案 0 :(得分:0)
a)为您的实体提供__toString()方法。
b)制作UserType。您也可以使用CLI命令bin/console doctrine:generate:crud
并选择“写入操作”[是]。
该脚本将生成一个控制器,一个formType和一些模板。
c)在UserType:
的顶部添加此use语句use Symfony\Bridge\Doctrine\Form\Type\EntityType;
d)将此表单字段添加到您的UserType:
->add('roles', EntityType::class, array(
'expanded => true,
'multiple' => true
))