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