You can clone with
HTTPS or Subversion.
It would be very useful if the entity form type, with "multiple" enabled, could accept an array from the bound object in addition to a Collection. Consider my User class, which is used in the
implements UserInterface, ...
/** @var ArrayCollection<Role> */
public function getRoles()
public function addRole(Role $role)
$this->roles = $role;
public function removeRole(Role $role)
And my form type for editing the user:
public function buildForm(FormBuilderInterface $builder, array $options)
// other fields ...
->add('roles', 'entity', array(
'class' => 'MySecurityBundle:Role',
'multiple' => true,
'expanded' => true,
If getRoles() returns an array (as shown above), the form component
throws this exception when I try to change the user's roles:
Expected argument of type "Doctrine\Common\Collections\Collection", "array" given
If I change getRoles to return a Collection, then the authentication
system throws this exception:
Catchable Fatal Error: Argument 4 passed to
must be an array, object given
In addition to consistency with other parts of the system (like the authentication system), another reason for my request is that returning an array instead of a collection is a convenient way to perform a defensive copy, so that clients of the User class do not have access to its internal state.
I have this very same problem, and also rather curious as to how to fix or circumvent it. Anyone out there care to help us out?
Just found a rather old (2 years!) issue that exactly described this problem: it seems you should make a separate method in your UserEntity like 'getRolesCollection'. This method should be made to only return the ArrayCollection itself. The method will be called if you rename your fieldname in the formbuilder accordingly (in this case 'rolesCollection')
Check out the original issue first; #1909
This fixed the issue for me, though I agree with the original poster; I'm surprised how this is not obvious or made clear somewhere at all, it seems such a common thing to do...
@sarcasme Do you think additional documentation in the security chapter would allow to close this issue ? I'll gladly try to create a PR for it.
@gnutix did you create the PR? could you please reference it back to this ticket?
ping what is the status of this
@cordoval Unfortunately, it's been months since I have post-its on my desk for OSS stuff (including this) and had not found anytime for it. I hope to catch up a little bit during december (will be off work for a while).
But if you want to handle this one, feel free to do so. Otherwise, I'll give my best to close it before Christmas.
This was fixed here: