2017 © Pedro Peláez
 

library data-fixtures

Data Fixtures for all Doctrine Object Managers

image

doctrine/data-fixtures

Data Fixtures for all Doctrine Object Managers

  • Friday, June 15, 2018
  • by beberlei
  • Repository
  • 38 Watchers
  • 839 Stars
  • 16,977,477 Installations
  • PHP
  • 348 Dependents
  • 45 Suggesters
  • 208 Forks
  • 45 Open issues
  • 18 Versions
  • 6 % Grown

The README.md

Doctrine Data Fixtures Extension

Build Status, (*1)

This extension aims to provide a simple way to manage and execute the loading of data fixtures for the Doctrine ORM or ODM. You can write fixture classes by implementing the Doctrine\Common\DataFixtures\FixtureInterface interface:, (*2)

namespace MyDataFixtures;

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

class UserDataLoader implements FixtureInterface
{
    public function load(ObjectManager $manager)
    {
        $user = new User();
        $user->setUsername('jwage');
        $user->setPassword('test');

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

Now you can begin adding the fixtures to a loader instance:, (*3)

use Doctrine\Common\DataFixtures\Loader;
use MyDataFixtures\UserDataLoader;

$loader = new Loader();
$loader->addFixture(new UserDataLoader());

You can load a set of fixtures from a directory as well:, (*4)

$loader->loadFromDirectory('/path/to/MyDataFixtures');

Or you can load a set of fixtures from a file:, (*5)

$loader->loadFromFile('/path/to/MyDataFixtures/MyFixture1.php');

You can get the added fixtures using the getFixtures() method:, (*6)

$fixtures = $loader->getFixtures();

Now you can easily execute the fixtures:, (*7)

use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;

$purger = new ORMPurger();
$executor = new ORMExecutor($em, $purger);
$executor->execute($loader->getFixtures());

If you want to append the fixtures instead of purging before loading then pass true to the 2nd argument of execute:, (*8)

$executor->execute($loader->getFixtures(), true);

Sharing objects between fixtures

In case if fixture objects have relations to other fixtures, it is now possible to easily add a reference to that object by name and later reference it to form a relation. Here is an example fixtures for Role and User relation, (*9)

namespace MyDataFixtures;

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

class UserRoleDataLoader extends AbstractFixture
{
    public function load(ObjectManager $manager)
    {
        $adminRole = new Role();
        $adminRole->setName('admin');

        $anonymousRole = new Role();
        $anonymousRole->setName('anonymous');

        $manager->persist($adminRole);
        $manager->persist($anonymousRole);
        $manager->flush();

        // store reference to admin role for User relation to Role
        $this->addReference('admin-role', $adminRole);
    }
}

And the User data loading fixture:, (*10)

namespace MyDataFixtures;

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

class UserDataLoader extends AbstractFixture
{
    public function load(ObjectManager $manager)
    {
        $user = new User();
        $user->setUsername('jwage');
        $user->setPassword('test');
        $user->setRole(
            $this->getReference('admin-role') // load the stored reference
        );

        $manager->persist($user);
        $manager->flush();

        // store reference of admin-user for other Fixtures
        $this->addReference('admin-user', $user);
    }
}

Fixture ordering

Notice that the fixture loading order is important! To handle it manually implement one of the following interfaces:, (*11)

OrderedFixtureInterface

Set the order manually:, (*12)

namespace MyDataFixtures;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Persistence\ObjectManager;

class MyFixture extends AbstractFixture implements OrderedFixtureInterface
{
    public function load(ObjectManager $manager)
    {}

    public function getOrder()
    {
        return 10; // number in which order to load fixtures
    }
}

DependentFixtureInterface

Provide an array of fixture class names:, (*13)

namespace MyDataFixtures;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;

class MyFixture extends AbstractFixture implements DependentFixtureInterface
{
    public function load(ObjectManager $manager)
    {}

    public function getDependencies()
    {
        return array('MyDataFixtures\MyOtherFixture'); // fixture classes fixture is dependent on
    }
}

class MyOtherFixture extends AbstractFixture
{
    public function load(ObjectManager $manager)
    {}
}

Notice the ordering is relevant to Loader class., (*14)

Running the tests:

Phpunit is included in the dev requirements of this package., (*15)

To setup and run tests follow these steps:, (*16)

  • go to the root directory of data-fixtures
  • run: composer install --dev
  • run: vendor/bin/phpunit

The Versions

20/03 2018

dev-master

9999999-dev http://www.doctrine-project.org

Data Fixtures for all Doctrine Object Managers

  Sources   Download

MIT

The Requires

 

The Development Requires

database

20/03 2018

v1.3.1

1.3.1.0 http://www.doctrine-project.org

Data Fixtures for all Doctrine Object Managers

  Sources   Download

MIT

The Requires

 

The Development Requires

database

27/11 2017

v1.3.0

1.3.0.0 http://www.doctrine-project.org

Data Fixtures for all Doctrine Object Managers

  Sources   Download

MIT

The Requires

 

The Development Requires

database

20/07 2017

2.0.x-dev

2.0.9999999.9999999-dev http://www.doctrine-project.org

Data Fixtures for all Doctrine tools

  Sources   Download

MIT

The Requires

 

The Development Requires

database persistence fixture

20/09 2016

v1.2.2

1.2.2.0 http://www.doctrine-project.org

Data Fixtures for all Doctrine Object Managers

  Sources   Download

MIT

The Requires

 

The Development Requires

database

20/06 2016

v1.2.1

1.2.1.0 http://www.doctrine-project.org

Data Fixtures for all Doctrine Object Managers

  Sources   Download

MIT

The Requires

 

The Development Requires

database

19/06 2016

v1.2.0

1.2.0.0 http://www.doctrine-project.org

Data Fixtures for all Doctrine Object Managers

  Sources   Download

MIT

The Requires

 

The Development Requires

database

30/03 2015

1.0.x-dev

1.0.9999999.9999999-dev http://www.doctrine-project.org

Data Fixtures for all Doctrine Object Managers

  Sources   Download

MIT

The Requires

 

The Development Requires

database

30/03 2015

v1.1.1

1.1.1.0 http://www.doctrine-project.org

Data Fixtures for all Doctrine Object Managers

  Sources   Download

MIT

The Requires

 

The Development Requires

database

28/03 2015

v1.1.0

1.1.0.0 http://www.doctrine-project.org

Data Fixtures for all Doctrine Object Managers

  Sources   Download

MIT

The Requires

 

The Development Requires

database

27/03 2015

v1.0.2

1.0.2.0 http://www.doctrine-project.org

Data Fixtures for all Doctrine Object Managers

  Sources   Download

MIT

The Requires

 

The Development Requires

database

23/03 2015

v1.0.1

1.0.1.0 http://www.doctrine-project.org

Data Fixtures for all Doctrine Object Managers

  Sources   Download

MIT

The Requires

 

The Development Requires

database

10/07 2013

v1.0.0

1.0.0.0 http://www.doctrine-project.org

Data Fixtures for all Doctrine Object Managers

  Sources   Download

MIT

The Requires

 

The Development Requires

database

10/07 2013

v1.0.0-ALPHA4

1.0.0.0-alpha4 http://www.doctrine-project.org

Data Fixtures for all Doctrine Object Managers

  Sources   Download

MIT

The Requires

 

The Development Requires

database

20/09 2012

v1.0.0-ALPHA3

1.0.0.0-alpha3 http://www.doctrine-project.org

Data Fixtures for all Doctrine Object Managers

  Sources   Download

MIT

The Requires

  • php >=5.3.2

 

database

25/01 2012

v1.0.0-ALPHA2

1.0.0.0-alpha2 http://www.doctrine-project.org

Data Fixtures for all Doctrine Object Managers

  Sources   Download

LGPL

The Requires

  • php >=5.3.2

 

database

12/01 2012

v1.0.0-ALPHA1

1.0.0.0-alpha1 http://www.doctrine-project.org

Data Fixtures for all Doctrine Object Managers

  Sources   Download

LGPL

The Requires

  • php >=5.3.2

 

database