2013-08-04 24 views
0

我正在尝试使用角色选择来创建新的/编辑用户表单。 我设法让登录,但我很努力弄清楚如何使角色下拉工作。 (角色表具有2项:ROLE_ADMIN和ROLE_USER) 我的实体的配置是:编辑具有角色的用户表单

Service\SafetyBundle\Entity\User: 
    type: entity 
    table: user 
    repositoryClass: Service\SafetyBundle\Entity\UserRepository 
    manyToMany: 
     roles: 
     targetEntity: Role 
     joinTable: 
      name: user_role 
      joinColumns: 
      user_id: 
       referencedColumnName: id 
      inverseJoinColumns: 
      role_id: 
       referencedColumnName: id 
    fields: 
     id: 
      type: integer 
      id: true 
      generator: 
       strategy: AUTO 

     user_name: 
      type: string 
      length: '40' 

     user_surname: 
      type: string 
      length: '40' 

     password: 
      type: integer 
      length: '6' 

     salt: 
      type: string 
      length: '32' 

     user_created: 
      type: datetime 
      columnDefinition: TIMESTAMP DEFAULT CURRENT_TIMESTAMP 
    lifecycleCallbacks: { } 

Service\SafetyBundle\Entity\Role: 
    type: entity 
    table: null 
    repositoryClass: Service\SafetyBundle\Entity\RoleRepository 
    fields: 
     id: 
      type: integer 
      id: true 
      generator: 
       strategy: AUTO 
     name: 
      type: string 
      length: '30' 
    lifecycleCallbacks: { } 

我的用户(修剪)实体:

/** 
    * Add roles 
    * 
    * @param \Service\SafetyBundle\Entity\Role $roles 
    * @return User 
    */ 
    public function addRole(\Service\SafetyBundle\Entity\Role $roles) 
    { 
     $this->roles[] = $roles; 

     return $this; 
    } 
    /** 
    * Remove roles 
    * 
    * @param \Service\SafetyBundle\Entity\Role $roles 
    */ 
    public function removeRole(\Service\SafetyBundle\Entity\Role $roles) 
    { 
     $this->roles->removeElement($roles); 
    } 

    /** 
    * Get roles 
    * 
    * @return \Doctrine\Common\Collections\Collection 
    */ 
    public function getRoles() 
    { 
     return $this->roles->toArray(); 
    } 
    public function getRolesForm() 
    { 
     return $this->roles; 
    } 
    public function setRolesForm($role) 
    { 
     return $this->roles[]=$role; 
    } 
    /** 
    * @see \Serializable::serialize() 
    */ 
    public function serialize() 
    { 
     return serialize(array(
      $this->id, 
     )); 
    } 

    /** 
    * @see \Serializable::unserialize() 
    */ 
    public function unserialize($serialized) 
    { 
     list (
      $this->id, 
     ) = unserialize($serialized); 
    } 
    public function eraseCredentials() 
    { 
    } 

形式:

public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('user_name') 
      ->add('user_surname') 
      ->add('password') 
      ->add('roles') 
     ; 
     $builder->add('save', 'submit'); 
    } 

我已经尝试过使用rolesForm,如其他线程所示:

$builder 
     ->add('user_name') 
     ->add('user_surname') 
     ->add('password') 
     ->add('rolesForm') 
    ; 

但我只能得到一个空的输入,搜查,但不能老是看着办吧...任何帮助将apreciated

回答

1

角色是一个实体,所以你可以使用Entity field type

$builder 
     ->add('user_name') 
     ->add('user_surname') 
     ->add('password') 
     ->add('roles', 'entity', array(
      'class' => 'Service\SafetyBundle\Entity\Role', 
      'property' => 'name', 
     )); 
    ; 

这会将所有可用的角色添加到数据库中。您还可以将choices选项设置为$user->getRoles()之类的内容以使用该列表,或use a QueryBuilder instance来过滤可用角色。

+0

这实际上是我的下拉!谢谢,但在编辑时,我改变名称,例如它会尝试再次插入表中的角色:user_role –

+0

你是什么意思与“它会尝试再次插入角色”?我认为这不应该自动发生?也许你会在控制器的某个地方保存_Role_实体? – UrGuardian4ngel

+0

在编辑时,它会尝试再次插入用户和角色之间的关系,我会尝试在控制器中执行它,但不会使用$ form-> getData(); –

相关问题