Skip to content

Instantly share code, notes, and snippets.

@programarivm
Created May 11, 2023 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save programarivm/f21e45273f1a3542463b5168064f033a to your computer and use it in GitHub Desktop.
Save programarivm/f21e45273f1a3542463b5168064f033a to your computer and use it in GitHub Desktop.
<?php
namespace App\DataFixtures;
use App\Entity\User;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
use Faker\Factory;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
class UserFixtures extends Fixture
{
const N = 20;
private $encoder;
private $faker;
public function __construct(UserPasswordEncoderInterface $encoder)
{
$this->encoder = $encoder;
$this->faker = Factory::create();
$this->faker->addProvider(new \Faker\Provider\Internet($this->faker));
}
public function load(ObjectManager $manager)
{
for ($i = 0; $i < self::N; $i++) {
$user = new User();
$user->setUsername($this->faker->username)
->setEmail($this->faker->email)
->setPassword($this->encoder->encodePassword(
$user,
$this->faker->password
));
$manager->persist($user);
$this->addReference("user-$i", $user);
}
$manager->flush();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment