This tutorial has a new version, check it out!

Fixtures: For some dumb data

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $12.00

We have the bundle! Plug it in! Open up the AppKernel class and add it there:

// app/AppKernel.php
// ...

$bundles = array(
    // ...
    new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(),
);

To see if it's working, try getting help information on a new doctrine:fixtures:load console task that comes from the bundle:

php app/console doctrine:fixtures:load --help

We see the help information, so we're ready to write some fixtures.

Writing Fixtures

A fixture is just a PHP class that puts some stuff into the database.

Create a new file in the DataFixtures\ORM directory of your bundle. Let's call it LoadEvents.php, though the name doesn't matter.

Create an src/Yoda/EventBundle/DataFixtures/ORM/LoadEvents.php file.

To breathe life into this, copy and paste the example from the docs. Change the namespace above the class to match our project. Notice that the namespace always follows the directory structure of the file:

// src/Yoda/EventBundle/DataFixtures/ORM/LoadEvents.php
namespace Yoda\EventBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;

class LoadEvents implements FixtureInterface
{
    public function load(ObjectManager $manager)
    {
        // .. todo
    }
}

Now we just use normal Doctrine code to create and save events. This is the play.php file all over again:

use Yoda\EventBundle\Entity\Event;
// ...

public function load(ObjectManager $manager)
{
    $event1 = new Event();
    $event1->setName('Darth\'s Birthday Party!');
    $event1->setLocation('Deathstar');
    $event1->setTime(new \DateTime('tomorrow noon'));
    $event1->setDetails('Ha! Darth HATES surprises!!!');
    $manager->persist($event1);

    $event2 = new Event();
    $event2->setName('Rebellion Fundraiser Bake Sale!');
    $event2->setLocation('Endor');
    $event2->setTime(new \DateTime('Thursday noon'));
    $event2->setDetails('Ewok pies! Support the rebellion!');
    $manager->persist($event2);

    // the queries aren't done until now
    $manager->flush();
}

Notice that we only need to call flush once. Doctrine prepares all of its work and then sends the queries as efficiently as possible all at once.

Loading the Fixtures

Ok, let's load some fixtures. Go back to the console and try the new doctrine:fixtures:load command:

php app/console doctrine:fixtures:load

When we look at the site, we've got fresh dummy data to play with. Re-run the command whenever you want to start over: it deletes everything and inserts the fixtures in a fresh state.

Tip

If you'd rather add to the existing data, just pass the --append option.

Leave a comment!

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=5.3.3",
        "symfony/symfony": "~2.4", // v2.4.2
        "doctrine/orm": "~2.2,>=2.2.3", // v2.4.2
        "doctrine/doctrine-bundle": "~1.2", // v1.2.0
        "twig/extensions": "~1.0", // v1.0.1
        "symfony/assetic-bundle": "~2.3", // v2.3.0
        "symfony/swiftmailer-bundle": "~2.3", // v2.3.5
        "symfony/monolog-bundle": "~2.4", // v2.5.0
        "sensio/distribution-bundle": "~2.3", // v2.3.4
        "sensio/framework-extra-bundle": "~3.0", // v3.0.0
        "sensio/generator-bundle": "~2.3", // v2.3.4
        "incenteev/composer-parameter-handler": "~2.0", // v2.1.0
        "doctrine/doctrine-fixtures-bundle": "~2.2.0" // v2.2.0
    }
}