I've implemented a group selection in my registration form (FOSUserBundle). After I submit my form Symfony will map my Types wrong. The goal is to add the selected group to the User through a registration form.
Here is my User entity:
class User extends BaseUser
{
protected $groups;
public function __construct()
{
parent::__construct();
$this->groups = new \Doctrine\Common\Collections\ArrayCollection();
}
public function addGroup(\FOS\UserBundle\Model\GroupInterface $groups)
{
$this->groups[] = $groups;
return $this;
}
public function removeGroup(\FOS\UserBundle\Model\GroupInterface $groups)
{
$this->groups->removeElement($groups);
}
public function getGroups()
{
return $this->groups;
}
}
Here is the relevant part from my form type.
$builder->add('groups', 'entity', array(
'label' => 'Type',
'required' => true,
'class' => 'xxUserBundle:Group',
'property' => 'name',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('g')->where('g.locked = false');
}
))
After submit Symfony will throw the following exception.
Neither the property "groups" nor one of the methods "setGroups()", "_set()" or "_call()" exist and have public access in class "xx\UserBundle\Entity\User".
The Exception is throwen in. /vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php at line 376
For an array property, that means any manyToXX
relation in doctrine, there is not setter method auto generated, for an array always an add
method will be generated instead to set
. Why does Symfony not find the right methods?
A temporary solution is to add
the method
public function setGroups(\FOS\UserBundle\Model\GroupInterface $groups)
{
return $this->addGroup($groups);
}
to the User entity. But in my opinion that is not the right solution... Someone knows where the error is or whats happen?
I'm using Symfony version 2.4.1
Thanks.