sajad torkamani

Quickstart

Assuming you’re using Symfony Flex, run:

composer require --dev orm-fixtures

Write example fixture:

// src/DataFixtures/AppFixtures.php
namespace App\DataFixtures;

use App\Entity\Product;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;

class AppFixtures extends Fixture
{
    public function load(ObjectManager $manager): void
    {
        // create 20 products! Bam!
        for ($i = 0; $i < 20; $i++) {
            $product = new Product();
            $product->setName('product '.$i);
            $product->setPrice(mt_rand(10, 100));
            $manager->persist($product);
        }

        $manager->flush();
    }
}

Load fixtures and purge the database:

php bin/console doctrine:fixtures:load

Load fixtures but append the data (no purging):

php bin/console doctrine:fixtures:load --append

Recipes

Links

Tagged: Doctrine