[Symfony]: Data fixtures

1) Installation

composer require --dev doctrine/doctrine-fixtures-bundle

2) Fixtures class

<?php
# ./src/DataFixtures/PostFixtures.php

namespace App\DataFixtures;

use App\Entity\Post;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;

class PostFixtures extends Fixture
{
    public function load(ObjectManager $manager)
    {
        $plan = new Post();
        $plan->setName('My first post');
        $plan->setDescription('This is my first post');
        $manager->persist($plan);

        $manager->flush();
    }
}

3) Loading fixtures

php bin/console doctrine:fixtures:load
# Data will be purged using truncate statement
php bin/console doctrine:fixtures:load --purge-with-truncate

Majd Arbash

Leave a Reply