1
votes

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.

2
This would be time to use the collection form type.Lighthart
Show your code in action, pleaseVictor Bocharsky
I think collection is not the right form type. The user can choose an existing group. He cannot add a new group. In my opinion Symfony will not detect the manyToMany relation between user and groups. In general my form looks right, but acts wrong.createproblem

2 Answers

1
votes

I also did not want to use the collection form type. Symfony2, relation manyToMany in a Form gave me the answer to this. Add:

'multiple' => true

to the form entity

0
votes

You may be referring to the wrong class in 'class' => 'xxUserBundle:Group', but the entity you showed is the User entity. This should be the User entity?

I found it helpful to create your own form class as you can reuse it more often and actually think it makes debugging forms easier when you have more advanced forms including cases where a form contains an entity or collection. http://symfony.com/doc/current/book/forms.html