I am trying to make an new/edit user form with role selection. I managed to make the login but I am struggling to figure out how to make the role dropdown work. ( roles table have 2 entries: ROLE_ADMIN and ROLE_USER) My entity configuration is:
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: { }
My User(trimmed) entity:
/**
* 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()
{
}
Form:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('user_name')
->add('user_surname')
->add('password')
->add('roles')
;
$builder->add('save', 'submit');
}
I have tried with rolesForm, as indicated in other threads:
$builder
->add('user_name')
->add('user_surname')
->add('password')
->add('rolesForm')
;
But I only get an empty input, searched but can`t figure it out ... any help would be apreciated
Role is an entity, so you could use the Entity field type .
$builder
->add('user_name')
->add('user_surname')
->add('password')
->add('roles', 'entity', array(
'class' => 'Service\SafetyBundle\Entity\Role',
'property' => 'name',
));
;
This will add all of your available roles in the DB to the field. You can also set the choices
option to something like $user->getRoles()
to use that list, or use a QueryBuilder instance to filter the available roles.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.