Search code examples
phpsymfonyfosuserbundlesonata

FOSUserBundle adding a group to the user wont do anything


I am using FOSuser with SonataUserBundle and I am trying to add a user to the Clients group everytime someone registers, but it doesnt work. I dont get any errors, but I am not adding the group either... I tried it in two ways:

1) I overwritten the registrationController and made the confirmAction save the new group like this:

    /**
     * Tell the user his account is now confirmed
     */
    public function confirmedAction()
    {
     $repository = $em->getRepository('ApplicationSonataUserBundle:Group');
        $group = $repository->findOneByName('Clients');
        $em = $this->getDoctrine()->getEntityManager();
        $user = $this->getUser();
        $user->addGroup($group);
        $this->em->flush();
        $userManager = $this->get('fos_user.user_manager');
        $userManager->updateUser($user);
    }
}

2) Icreated an eventListener and made the groupping there:

<?php

namespace Application\Sonata\UserBundle\EventListener;

use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FormEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Doctrine\ORM\EntityManager;

/**
 * Listener responsible to change the redirection at the end of the password resetting
 */
class GrouppingListener implements EventSubscriberInterface
{
    protected $em;
    protected $user;

    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    /**
     * {@inheritDoc}
     */
    public static function getSubscribedEvents()
    {
        return array(
            FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',
        );
    }

    public function onRegistrationSuccess(FormEvent $event)
    {
        $this->user = $event->getForm()->getData();
        $entity = $this->em->getRepository('ApplicationSonataUserBundle:Group')->findOneByName('Clients'); // You could do that by Id, too
        $this->user->addGroup($entity);
        $this->em->flush();

    }
}

My group entity is being extended like this:

<?php

/**
 * This file is part of the <name> project.
 *
 * (c) <yourname> <youremail>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Application\Sonata\UserBundle\Entity;

use Sonata\UserBundle\Entity\BaseGroup as BaseGroup;

/**
 * This file has been generated by the Sonata EasyExtends bundle ( http://sonata-project.org/bundles/easy-extends )
 *
 * References :
 *   working with object : http://www.doctrine-project.org/projects/orm/2.0/docs/reference/working-with-objects/en
 *
 * @author <yourname> <youremail>
 */
class Group extends BaseGroup
{
    /**
     * @var integer $id
     */
    protected $id;

    /**
     * Get id
     *
     * @return integer $id
     */
    public function getId()
    {
        return $this->id;
    }
}

None of these options worked... I did this based on other stackoverflow answers...Why wont it work?


Solution

  • You're missing persist in both of your cases. In order for an entity to become manageable, it needs to be persisted first.

    $user->addGroup($group);
    $this->em->flush();
    

    change this in your controller action to:

    $user->addGroup($group);
    $this->em->persist($user);
    $this->em->flush();
    

    A paragraph from Doctrine manual:

    An entity can be made persistent by passing it to the EntityManager#persist($entity) method. By applying the persist operation on some entity, that entity becomes MANAGED, which means that its persistence is from now on managed by an EntityManager. As a result the persistent state of such an entity will subsequently be properly synchronized with the database when EntityManager#flush() is invoked.