Skip to content

Instantly share code, notes, and snippets.

@jmather
Last active June 15, 2021 18:26
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jmather/3813952 to your computer and use it in GitHub Desktop.
Save jmather/3813952 to your computer and use it in GitHub Desktop.
How to load users in a fixture
<?php
namespace Application\Sonata\UserBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Application\Sonata\UserBundle\Entity\User;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use FOS\UserBundle\Doctrine\UserManager;
class LoadUserData extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
{
private UserManager $userManager;
private UserPasswordEncoderInterface $passwordEncoder;
/**
* {@inheritDoc}
*/
public function setContainer(UserManager $userManager, UserPasswordEncoderInterface $passwordEncoder)
{
$this->userManager = $userManager;
$this->passwordEncoder = $passwordEncoder;
}
/**
* {@inheritDoc}
*/
public function load(ObjectManager $manager)
{
$test_password = 'test';
/** @var $user \Application\Sonata\UserBundle\Entity\User */
$user = $this->userManager->createUser();
$user->setUsername('superadmin');
$user->setEmail('superadmin@example.com');
$user->setFirstname('Super');
$user->setLastname('Admin');
$user->setRoles(array('ROLE_SUPER_ADMIN'));
$user->setEnabled(true);
$password = $this->passwordEncoder->encodePassword($test_password, $user->getSalt());
$user->setPassword($password);
$this->userManager->updateUser($user);
$this->addReference('user.super_admin', $user);
unset($user);
/** @var $user \Application\Sonata\UserBundle\Entity\User */
$user = $this->userManager->createUser();
$user->setUsername('admin');
$user->setPlainPassword($test_password);
$user->setEmail('admin@example.com');
$user->setFirstname('Regular');
$user->setLastname('Admin');
$user->setRoles(array('ROLE_ADMIN'));
$user->setEnabled(true);
$password = $this->passwordEncoder->encodePassword($test_password, $user->getSalt());
$user->setPassword($password);
$this->userManager->updateUser($user);
$this->addReference('user.admin', $user);
$faker = \Faker\Factory::create();
for ($i = 0; $i < 10; $i++)
{
/** @var $user \Application\Sonata\UserBundle\Entity\User */
$user = $this->userManager->createUser();
$user->setUsername($faker->userName);
$user->setPlainPassword($test_password);
$user->setEmail($faker->safeEmail);
$user->setFirstname($faker->firstName);
$user->setLastname($faker->lastName);
$user->setRoles(array('ROLE_USER'));
$user->setEnabled(true);
$password = $this->passwordEncoder->encodePassword($test_password, $user->getSalt());
$user->setPassword($password);
$this->userManager->updateUser($user);
$this->addReference('user.demo_'.$i, $user);
}
}
/**
* Get the order of this fixture
*
* @return integer
*/
function getOrder()
{
return 1;
}
}
@CyrilCharlier
Copy link

Thanks for this gist !

@Ravend6
Copy link

Ravend6 commented May 8, 2016

good job!

@RobinBastiaan
Copy link

RobinBastiaan commented Jun 15, 2021

Just a heads up for people trying to find a solution around the time of this comment: please note that this gist does not work for current versions of Symfony, as it would give you the following error:

  The "security.encoder_factory" service or alias has been removed or inlined when the container wa
  s compiled. You should either make it public, or stop using the container directly and use depend
  ency injection instead.

Instead you can use something like this:

    private UserPasswordEncoderInterface $passwordEncoder;

    public function __construct(UserPasswordEncoderInterface $passwordEncoder)
    {
        $this->passwordEncoder = $passwordEncoder;
    }

    public function load(ObjectManager $manager): void
    {

        ...

        $user->setPassword($this->passwordEncoder->encodePassword(
            $user,
            $thePlainPassword
        ));
        
        ...

    }

@jmather
Copy link
Author

jmather commented Jun 15, 2021

That's... pretty impressive that after 9 years all that changed was that i should have used more dependency injection. Totes my bad, but I'll take it for a one-off script. :) Thanks for the update!

@jmather
Copy link
Author

jmather commented Jun 15, 2021

I've tried to update it to incorporate the comment and kinda bump up it's stuff a little, but i've not touched PHP in a long time now, so YMMV on results. :)

@RobinBastiaan
Copy link

Awesome mate! It is to be expected coding standards change, and maybe even more impressive that Google still found this gist during a regular search. Thanks for sharing your code. Have a nice day!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment