Last active
August 22, 2020 20:01
-
-
Save 5quinque/c8c350e813b0d55c4b053b65faa3902c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\DataFixtures; | |
use App\Entity\User; | |
use Doctrine\Bundle\FixturesBundle\Fixture; | |
use Doctrine\Common\Persistence\ObjectManager; | |
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; | |
class UserFixtures extends Fixture | |
{ | |
private $passwordEncoder; | |
public const ADMIN_USER_REFERENCE = 'admin-user'; | |
public function __construct(UserPasswordEncoderInterface $passwordEncoder) | |
{ | |
$this->passwordEncoder = $passwordEncoder; | |
} | |
public function load(ObjectManager $manager) | |
{ | |
$admin = new User(); | |
$admin->setUsername('admin'); | |
$admin->setRoles(['ROLE_ADMIN', 'ROLE_MODERATOR']); | |
$admin->setPassword($this->passwordEncoder->encodePassword( | |
$admin, | |
'admin' | |
)); | |
$manager->persist($admin); | |
$this->addReference(self::ADMIN_USER_REFERENCE, $admin); | |
$manager->flush(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment