This tutorial has a new version, check it out!

Sharing Data between Fixture Classes

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

Sharing Data between Fixture Classes

Let’s update the fixtures so that each event has an owner.

We have two fixture classes: one that loads events and one that loads users.

Ordering how Fixtures are Loaded

Start in the LoadUsers class. Now that events depend on users, we’ll want this fixture class to be executed before the events class. To force this, implement a new interface called OrderedFixtureInterface. This requires one method called getOrder. Let’s return 10:

// src/Yoda/UserBundle/DataFixtures/ORM/LoadUsers.php
// ...

use Doctrine\Common\DataFixtures\OrderedFixtureInterface;

class LoadUsers implements FixtureInterface, ContainerAwareInterface, OrderedFixtureInterface
{
    // ...

    public function getOrder()
    {
        return 10;
    }
}

Head over to LoadEvents and make the same change, except returning 20 so that the class is run second:

// src/Yoda/EventBundle/DataFixtures/ORM/LoadEvents.php
// ...

use Doctrine\Common\DataFixtures\OrderedFixtureInterface;

class LoadEvents implements FixtureInterface, OrderedFixtureInterface
{
    // ...

    public function getOrder()
    {
        return 20;
    }
}

Assigning Owners in Fixtures

Now, we just need to get our new User objects inside LoadEvents. DoctrineFixturesBundle has a standard way of sharing data between fixtures, but a much easier way is just to query for our wayne user:

// src/Yoda/EventBundle/DataFixtures/ORM/LoadEvents.php
// ...

class LoadEvents implements FixtureInterface, OrderedFixtureInterface
{
    $wayne = $manager->getRepository('UserBundle:User')
        ->findOneByUsernameOrEmail('wayne');

    // ...
}

All we need to do now is call setOwner on both events so that it looks like wayne created them:

// src/Yoda/EventBundle/DataFixtures/ORM/LoadEvents.php
// ...
public function load(ObjectManager $manager)
{
    $wayne = $manager->getRepository('UserBundle:User')
        ->findOneByUsernameOrEmail('wayne');
    // ...

    $event1->setOwner($wayne);
    $event2->setOwner($wayne);

    // ...
    $manager->flush();
}

Ok! Reload the fixtures!

php app/console doctrine:fixtures:load

Now use app/console to check that each event has an owner:

php app/console doctrine:query:sql "SELECT * FROM yoda_event"

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
        "ircmaxell/password-compat": "~1.0.3", // 1.0.3
        "phpunit/phpunit": "~4.1", // 4.1.0
        "stof/doctrine-extensions-bundle": "~1.1.0" // v1.1.0
    }
}