2016-11-23 81 views
-2

我有一個問題,實現一個symfony 3表單與兩個對象具有多對多關係。symfony 3表格中的多對多關係

我有兩個數據表,「用戶」和「角色」。我想要一個表單,我可以在其中編輯用戶併爲用戶分配一些角色。詳細我想要一個表單,其中每個角色是一個複選框,我可以選擇用戶擁有哪個角色。 我知道,我必須實現一個新的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

一)給你的實體__toString()方法。

b)製作UserType。您也可以使用CLI命令bin/console doctrine:generate:crud並在「寫入操作」[是]處選擇。 該腳本將生成一個控制器,一個formType和一些模板。

c)在用戶類型的頂部添加此使用的語句:

use Symfony\Bridge\Doctrine\Form\Type\EntityType; 

d)添加此formfield到您的用戶類型:

->add('roles', EntityType::class, array(
    'expanded => true, 
    'multiple' => true 
)) 
+0

嗨,我覺得這工作得很好。但是現在我得到了一個'Warning:爲foreach()'提供的無效參數,因爲我修改了getRoles和getPermissions(我需要將所有字符串用於自動化)。你知道我怎麼能解決這個問題嗎?我需要使用isGranted(「ROLE _...」)檢查twig-templates中的角色,因此getRoles和getPermissions必須返回字符串數組。有沒有辦法解決這個問題? – user3296316

+0

爲foreach()提供的參數無效sais不提供數組。值可以爲空嗎? –