I'm trying to implement https://github.com/ZF-Commons/zfc-rbac in my module. At the moment I'm stuck, because I get following exception:
Argument 1 passed to User\Entity\User::setRoles() must implement interface Doctrine\Common\Collections\Collection, array given ...
Line, that causes the error in UserManager class: $user->setRoles($data['role']);
So it's clear, that setter in the entity is wrong or form element of type select which returns the $data['role']
element.
Code of setter:
public function setRoles(Collection $roles)
{
$this->roles->clear();
foreach ($roles as $role) {
$this->roles[] = $role;
}
}
Code of select element in UserForm:
$this->add([
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'name' => 'role',
'attributes' => [
'multiple' => true,
],
'options' => [
'object_manager' => $this->entityManager,
'target_class' => 'User\Entity\Role',
'label' => 'Role',
'value_options' => $roles,
'disable_inarray_validator' => true, //TODO: create validator
'required' => true,
],
]);
So how cane I make select element return Doctrine\Common\Collections\Collection?
I tried to use ArrayCollection from Doctrine namespace, but it didn't work. Do I have to make separate class, that implements Collection interface and use it with select element? Or maybe there is some more conveniant ways to achieve this?
I also tried with removed @var, @param annotations and types in the Entity, but in this case I've got following message:
Expected value of type "Doctrine\Common\Collections\Collection|array" for association field "User\Entity\User#$roles", got "string" instead.