我正在尝试使用角色选择制作一个新/编辑用户表单。我设法进行了登录,但我正在努力弄清楚如何使角色下拉菜单起作用。(角色表有 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')
;
但我只得到一个空输入,搜索但无法弄清楚......任何帮助都会受到赞赏