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')
    ;

但我只得到一个空输入,搜索但无法弄清楚......任何帮助都会受到赞赏

4

1 回答 1

1

Role是一个实体,因此您可以使用Entity字段类型

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

这会将您在数据库中的所有可用角色添加到该字段。您还可以将choices选项设置$user->getRoles()为使用该列表,或使用 QueryBuilder 实例来过滤可用角色。

于 2013-08-04T15:02:24.950 回答