FOSUserBundle: How to create User manually

5,771 views
Skip to first unread message

Jehu

unread,
Aug 16, 2011, 6:42:35 AM8/16/11
to FriendsOfSymfony Developer
Hi,

i've a Customer Entity with an assiciated User Entity. The User class
extends FOS\UserBundle\Entity\User
On Signup i've to do some more things than just create a new User. I
want to create a Customer with one User (can have many) and some
further things (e. g. create Random Password, send Mail for "double
opt in" and so on).

My question is: How can i create a User manually?

$userManager = $this->get('fos_user.user_manager');
$user = $userManager->createUser();

$user->setEmail('te...@blub.com');
$user->setPlainPassword('12345'); // should be random password

$customer = new Customer();
// .... set some customer properties
$customer->addUsers($user);

$em = $this->getDoctrine()->getEntityManager();
$em->persist($customer);
$em->flush();

But there are some errors by doing this, e.g.:
Integrity constraint violation: 1048 Column 'salt' cannot be null


The i've tried the code below instead of user_manager-Service
"createUser()"-method:

$userManipulator = $this->get('fos_user.util.user_manipulator');
$user = $userManipulator->create(
'Test',
'testpassword',
'te...@mail.com',
false,
false
);

The error message is:
Warning: array_search() expects parameter 2 to be array, null given
in /the/full/path/vendor/bundles/FOS/UserBundle/Model/User.php line
507

It would be nice to have some documentation how to create users
programatically in own code/controller.
Can someone help me?

Tnx
Jehu

Christophe COEVOET

unread,
Aug 16, 2011, 6:49:39 AM8/16/11
to friendsofs...@googlegroups.com
Le 16/08/2011 12:42, Jehu a �crit :

> Hi,
>
> i've a Customer Entity with an assiciated User Entity. The User class
> extends FOS\UserBundle\Entity\User
> On Signup i've to do some more things than just create a new User. I
> want to create a Customer with one User (can have many) and some
> further things (e. g. create Random Password, send Mail for "double
> opt in" and so on).
>
> My question is: How can i create a User manually?
>
> $userManager = $this->get('fos_user.user_manager');
> $user = $userManager->createUser();
>
> $user->setEmail('te...@blub.com');
> $user->setPlainPassword('12345'); // should be random password
>
> $customer = new Customer();
> // .... set some customer properties
> $customer->addUsers($user);
>
> $em = $this->getDoctrine()->getEntityManager();
> $em->persist($customer);
> $em->flush();
>
> But there are some errors by doing this, e.g.:
> Integrity constraint violation: 1048 Column 'salt' cannot be null
this error occurs when you don't call the constructor (which generates
the salt). Make sure to call the parent constructor when you overwrite
it to add your own logic (as stated in the documentation)

>
>
> The i've tried the code below instead of user_manager-Service
> "createUser()"-method:
>
> $userManipulator = $this->get('fos_user.util.user_manipulator');
> $user = $userManipulator->create(
> 'Test',
> 'testpassword',
> 'te...@mail.com',
> false,
> false
> );
>
> The error message is:
> Warning: array_search() expects parameter 2 to be array, null given
> in /the/full/path/vendor/bundles/FOS/UserBundle/Model/User.php line
> 507
>
> It would be nice to have some documentation how to create users
> programatically in own code/controller.
> Can someone help me?
>
> Tnx
> Jehu


--
Christophe | Stof

Jehu

unread,
Aug 16, 2011, 7:12:09 AM8/16/11
to FriendsOfSymfony Developer
Thank you so much. That's it!

Jehu
Reply all
Reply to author
Forward
Message has been deleted
0 new messages