I have two entites User Role. Both are used in Symfony2 for authentication. The authentication process needs to have method ‘getRoles’ in class User which returns array.
On the other hand there is form builder which needs roles given as Collection.
How can i force form builder to use specific getter to get Collection of roles?
class UserType extends AbstractType{
//...
->add('roles', 'entity', array( 'class' => 'MyBundle:Role',
'property' => 'name',
'required' => false,
'multiple' => true,
'expanded' => true,
);
//...
}
class User implements UserInterface {
//...
public function getRoles() {
return $this->roles->toArray(); //This needs authentication mechanism
}
public function getRolesCollection() {
return $this->roles; //This needs form builder.
}
//...
}
Maybe you could try to invert it all :