Search code examples
phpsymfonyfixturesfaker

PHP Symfony Fixtures & Faker


I started learning php symfony, and I had a question. I have two entities : Ideas joined with another enitie Category I created a fixture to populate my database, but I'm stuck at one point. It's when I do ->setCategory(); how do I inject random categories ( I have Travel, sport, Entertainement, Human relation, Others). Here's how I started, and stuck for the random category of TYPE Category. Thanks for your help

    public function load(ObjectManager $manager)
{
    // $product = new Product();
    // $manager->persist($product);

    $generator = Faker\Factory::create("fr_FR");

    for ($i = 0; $i < 101; $i++) {
        $idea = new Idea();
        $idea->setTitle($generator->title);
        $idea->setDescription($generator->realText(200));
        $idea->setAuthor($generator->firstName);
 --------- $idea->setCategory("RANDOM CATEGORY");---------
        $idea->setDateCreated($generator->dateTime);
        $idea->setIsPublished(true);

        $manager->persist($idea);
    }
        $manager->flush();
}

Solution

  • If your categories are Entities like this suggests:

    Here's how I started, and stuck for the random category of TYPE Category.

    ... and you have different fixtures for your different entities, you could leverage the DependentFixtureInterface.

    CategoryFixtures could look something like this.

    <?php
    
    namespace App\DataFixtures;
    
    use App\Entity\Category;
    use Doctrine\Bundle\FixturesBundle\Fixture;
    use Doctrine\Persistence\ObjectManager;
    use Faker\Factory;
    
    class CategoryFixtures extends Fixture
    {
        public function load(ObjectManager $manager)
        {
            $generator = Factory::create("fr_FR");
    
            for ($i = 0; $i <= 100; $i++) {
                $category = (new Category())->setName($generator->name);
                $manager->persist($category);
            }
            $manager->flush();
        }
    }
    

    And then your IdeaFixtures could look like this.

    <?php
    
    namespace App\DataFixtures;
    
    use App\Entity\Category;
    use App\Entity\Idea;
    use Doctrine\Bundle\FixturesBundle\Fixture;
    use Doctrine\Common\DataFixtures\DependentFixtureInterface;
    use Doctrine\Persistence\ObjectManager;
    use Faker\Factory;
    
    class IdeaFixtures extends Fixture implements DependentFixtureInterface
    {
        public function load(ObjectManager $manager)
        {
            $generator  = Factory::create("fr_FR");
            $categories = $manager->getRepository(Category::class)->findAll();
    
            for ($i = 0; $i <= 100; $i++) {
                $idea = new Idea();
                $idea->setTitle($generator->title);
                $idea->setDescription($generator->realText(200));
                $idea->setAuthor($generator->firstName);
                $idea->setCategory($generator->randomElement($categories));
                $idea->setDateCreated($generator->dateTime);
                $idea->setIsPublished(true);
    
                $manager->persist($idea);
            }
            $manager->flush();
        }
    
        public function getDependencies(): array
        {
            return [CategoryFixtures::class];
        }
    }
    

    This way you ensure that the CategoryFixtures will be loaded first, then you will be able to fetch all categories in the IdeaFixtures and get a random one leveraging the Faker randomElement() method.