2017-01-05 6 views
0

どのようにしてDBにロールを保存できますか? 私は Symfony/FosUserBundle - DBにロールを格納する

/** 
* Role Entity 
* 
* @ORM\Entity 
* @ORM\Table(name="roles") 
*/ 
class Role implements RoleInterface 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer", name="id") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 
    /** 
    * @ORM\Column(type="string", name="role", unique=true, length=70) 
    */ 
    private $role; 

    /** 
    * Populate the role field 
    * @param string $role ROLE_FOO etc 
    */ 
    public function __construct($role) 
    { 
     $this->role = $role; 
    } 

    /** 
    * Return the role field. 
    * @return string 
    */ 
    public function getRole() 
    { 
     return $this->role; 
    } 

    /** 
    * Return the role field. 
    * @return string 
    */ 
    public function __toString() 
    { 
     return (string)$this->role; 
    } 
} 

私はいつもこのエラーを取得していROLE.php

USER.php

この方法に
/** 
* @ORM\Entity 
* @ORM\Table(name="`user`") 
*/ 
class User extends BaseUser 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    /** 
    * @ORM\OneToMany(targetEntity="Post", mappedBy="author") 
    */ 
    public $article; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="full_name", type="string", length=255, nullable=true) 
    */ 
    public $name; 

    /** 
    * @ORM\ManyToMany(targetEntity="Role") 
    * @ORM\JoinTable(name="users_roles") 
    */ 
    protected $roles; 

    public function __construct() 
    { 
     parent::__construct(); 
     $this->roles = new ArrayCollection(); 
    } 

    ....settings... 

    /** 
    * Returns an ARRAY of Role objects with the default Role object appended. 
    * @return array 
    */ 
    public function getRoles() 
    { 
     return array_merge($this->roles->toArray(), array(new Role(parent::ROLE_DEFAULT))); 
    } 

    /** 
    * Returns the true ArrayCollection of Roles. 
    * @return ArrayCollection 
    */ 
    public function getRolesCollection() 
    { 
     return $this->roles; 
    } 

    /** 
    * Pass a string, get the desired Role object or null. 
    * @param string $role 
    * @return Role|null 
    */ 
    public function getRole($role) 
    { 
     foreach ($this->getRoles() as $roleItem) { 
      if ($role == $roleItem->getRole()) { 
       return $roleItem; 
      } 
     } 
     return null; 
    } 

    /** 
    * Pass a string, checks if we have that Role. Same functionality as getRole() except returns a real boolean. 
    * @param string $role 
    * @return boolean 
    */ 
    public function hasRole($role) 
    { 
     if ($this->getRole($role)) { 
      return true; 
     } 
     return false; 
    } 

    /** 
    * Adds a Role OBJECT to the ArrayCollection. Can't type hint due to interface so throws Exception. 
    * @throws Exception 
    * @param Role $role 
    */ 
    public function addRole($role) 
    { 
     if (!$role instanceof Role) { 
      throw new \Exception("addRole takes a Role object as the parameter"); 
     } 

     if (!$this->hasRole($role->getRole())) { 
      $this->roles->add($role); 
     } 
    } 

    /** 
    * Pass a string, remove the Role object from collection. 
    * @param string $role 
    */ 
    public function removeRole($role) 
    { 
     $roleElement = $this->getRole($role); 
     if ($roleElement) { 
      $this->roles->removeElement($roleElement); 
     } 
    } 

    /** 
    * Pass an ARRAY of Role objects and will clear the collection and re-set it with new Roles. 
    * Type hinted array due to interface. 
    * @param array $roles Of Role objects. 
    */ 
    public function setRoles(array $roles) 
    { 
     $this->roles->clear(); 
     foreach ($roles as $role) { 
      $this->addRole($role); 
     } 
    } 

    /** 
    * Directly set the ArrayCollection of Roles. Type hinted as Collection which is the parent of (Array|Persistent)Collection. 
    * @param Collection $role 
    */ 
    public function setRolesCollection(Collection $collection) 
    { 
     $this->roles = $collection; 
    } 
} 

http://blog.jmoz.co.uk/symfony2-fosuserbundle-role-entities/

を試してみました。

[教義\ ORM \マッピング\ MappingException] "*** \エンティティ\ユーザー" の
プロパティ "役割" はすでに が宣言されましたが、それは一度だけ宣言されなければならない

でした誰かが私を助けてくれますか?あなたのUserエンティティ内部rolesに関連するすべてのものを取り除くために必要

おかげ

+0

FosUserBundleに関する最新の記事を見つけ、それに従ってください。バンドルはボックス外の役割をサポートしていますので、どこにも定義するべきではありません。 – Baig

+0

それは問題です、私は最新の記事を見つけることができません。 –

+1

あなたのエンティティを共有してみましょう。人々は見た目と助けを得ることができます – Baig

答えて

0

。ロールは、バンドルからすぐにサポートされます。

バンドルをセットアップするだけで、すべて正常に動作しています。これは私のUserエンティティがバンドルは、私は、スキーマの更新コマンドを実行した、setupだったらBaseUser

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use FOS\UserBundle\Model\User as BaseUser; 
/** 
* User 
* 
* @ORM\Table(name="user") 
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository") 
*/ 
class User extends BaseUser 
{ 
    /** 
    * @var int 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 
    public function __construct() 
    { 
     parent::__construct(); 
     // your own logic 
    } 

    /** 
    * Get id 
    * 
    * @return int 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

} 

を拡張しているように見えるものです。

ロールのように、columnというロールがバンドルを介して自動的に追加されます。

enter image description here

あなたはcommandを使用してユーザーを作成することが付属しているバンドルで遊ぶことができます。

バンドルのドキュメントはhereです。 Hereは、あなたを始められるもう一つの良いリソースです。

+0

ええ、私はすべての役割のために特別なテーブルが必要です。私はsecurity.ymlを使いたくない。私はsecurity.ymlへのアクセスを与えたくないからです。 私はCMSを作成しようとしています。何とかしたら管理者が新しい役割を作成できるようにする必要があります。 –

+0

その後、fos user bundleを削除して、カスタムバンドルを作成します。 – Baig

関連する問題