Herr Nentu'
Herr Nentu'

Reputation: 1506

Symfony FOSUserBundle - Create custom repository to persist users

I am using FOSUserBundle to be able to manage users in a symfony2 project.

Since using container is not recommended my question is how can I extend the FOSUserBundle to be able to create a custom save method like this, for example:

class UserRepository extends EntityRepository
{                                            
    public function registration(array $data)                               
    {     
    // example only
    $userManager = $this->container->get('fos_user.user_manager');      
    //$em = $this->container->get('doctrine')->getEntityManager();        

    //$userUtils = $this->container->get('fos_user.util.token_generator');

    $user = $userManager->createUser();                                 

    $user->setFirstName($data['first_name']);                           
    $user->setLastName($data['last_name']);                             
    $user->setEmail($data['user_email']);                               
    $user->setUsername($data['user_email']);                            
    $user->setPlainPassword($data['user_password']);                    
    $user->setEnabled(false);                                           
    $user->setConfirmationToken($userUtils->generateToken());           
    $user->addRole('ROLE_USER');                                        

    $em->persist($user);                                                

    $em->flush();                                                       
}

Would it be smart to pass the $userManager and $userUtils objects in the controller when using the method?

Upvotes: 0

Views: 1122

Answers (1)

Smashou
Smashou

Reputation: 378

I think the better is to override the FosUser Controller Action (Register for example) and put your code in a specific service.

The symfony2 doc give a great sample: http://symfony.com/doc/current/cookbook/bundles/inheritance.html

Upvotes: 3

Related Questions