使用角色编辑用户表单

时间:2013-08-04 13:58:00

标签: php symfony user-roles

我正在尝试使用角色选择制作新的/编辑用户表单。 我设法登录但我正在努力弄清楚如何使角色下拉列表工作。 (角色表有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')
    ;

但我只得到一个空的输入,搜索但是无法弄明白......任何帮助都会被贬低

1 个答案:

答案 0 :(得分: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来过滤可用的角色。