Latest release: v0.3

Fixtures

Content

Usage

At first you should register FixturesExtension at your config file.

extensions:
    fixtures: Nettrine\Fixtures\DI\FixturesExtension
1
2

This extension is highly depending on Symfony\Console, it does not make sence to use it without Console. Take a look at simple Contributte/Console integration.

composer require contributte/console
1
extensions:
    console: Contributte\Console\DI\ConsoleExtension
1
2

Configuration

You should define paths where the fixture classes are stored.

fixtures:
    paths:
        - app/model/Fixtures
        - ...
1
2
3
4

Command

The doctrine:fixtures:load command loads data fixtures from your configuration by default:

doctrine:fixtures:load
1

You can also optionally specify the path to the fixtures with the --fixtures option:

doctrine:fixtures:load --fixtures=/path/to/fixtures1 --fixtures=/path/to/fixtures2
1

If you want to append the fixtures instead of flushing the database first you can use the --append option:

doctrine:fixtures:load --append
1

By default Doctrine Fixtures uses DELETE statements to drop the existing rows from the database. If you want to use a TRUNCATE statement instead, you can use the --purge-with-truncate flag:

doctrine:fixtures:load --purge-with-truncate
1

Fixture

Simpliest fixture implements just Doctrine\Common\DataFixtures\FixtureInterface

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

class Foo1Fixture implements FixtureInterface
{

	/**
	 * Load data fixtures with the passed ObjectManager
	 *
	 * @param ObjectManager $manager
	 * @return void
	 */
	public function load(ObjectManager $manager)
	{
		// TODO: Implement load() method.
	}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

If you need use ordering, implement Doctrine\Common\DataFixtures\OrderedFixtureInterface

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

class Foo2Fixture implements FixtureInterface, OrderedFixtureInterface
{

	/**
	 * Load data fixtures with the passed ObjectManager
	 *
	 * @param ObjectManager $manager
	 * @return void
	 */
	public function load(ObjectManager $manager)
	{
		// TODO: Implement load() method.
	}

	/**
	 * Get the order of this fixture
	 *
	 * @return int
	 */
	public function getOrder()
	{
		return 1;
	}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

If you need use referencing, extend Doctrine\Common\DataFixtures\AbstractFixture

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

class Foo3Fixture extends AbstractFixture
{

	/**
	 * Load data fixtures with the passed ObjectManager
	 *
	 * @param ObjectManager $manager
	 */
	public function load(ObjectManager $manager)
	{
		// TODO: Implement load() method.
		$this->addReference('user', new User());
		$this->getReference('user');
	}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

If you need use container, implement Nettrine\Fixtures\ContainerAwareInterface


use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Nette\DI\Container;

class Foo4Fixture implements FixtureInterface, ContainerAwareInterface
{

	/** @var Container */
	private $container;

	/**
	 * @param Container $container
	 * @return void
	 */
	public function setContainer(Container $container)
	{
		$this->container = $container;
	}

	/**
	 * Load data fixtures with the passed ObjectManager
	 *
	 * @param ObjectManager $manager
	 */
	public function load(ObjectManager $manager)
	{
		// TODO: Implement load() method.
		$this->container->getService('foo');
	}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32