Reputation: 5643
I'm creating the admin view where i can list the users and create the users.
I have created the User
class but i am not sure how can i persist it.
Do I need to manually create the form and then persist it?
I have saved using normal save but then there is validation performed.
I want to know wether I need to manually encode password etc. or FOSUserBundle
will do that for me.
Upvotes: 3
Views: 2060
Reputation: 52513
I assume that your aren't planning to use ...
/register
route / method for new users app/console fos:user:create testuser [email protected] p@ssword
... as you're talking about an admin (web)-interface.
Answer:
In a ContainerAware
class (i.e. controllers) ...
... you can re-use the default registration-form and disable validation:
$user = new User();
$this->createForm(
$this->get('fos_user.registration.form.type'),
$user,
array(
'validation_groups' => false,
)
);
Here's how to manually create a new User
and persist it to the database:
$om = $this->container->get('doctrine.orm.entity_manager');
$manager = $this->container->get('fos_user.user_manager');
$user = $manager->createUser();
$user
->setUsername('nifr')
->setEmail('[email protected]')
->setPlainPassword('Insecure123')
->setEnabled(true)
;
$om->persist($user);
$om->flush();
Upvotes: 4