Contributte Doctrine-fixtures
Doctrine/DataFixtures (opens new window) for Nette Framework
# Content
# Setup
Install package
composer require nettrine/fixtures
Register extension
extensions:
nettrine.fixtures: Nettrine\Fixtures\DI\FixturesExtension
2
# Relying
Take advantage of enpowering this package with 2 extra packages:
doctrine/orm
symfony/console
# doctrine/orm
This package relies on doctrine/orm
, use prepared nettrine/orm (opens new window) integration.
Doctrine ORM depends on Doctrine DBAL, use prepared nettrine/dbal (opens new window) integration
composer require nettrine/dbal
composer require nettrine/orm
2
Without these packages the fixtures can't be processed, because they need a database connection and entities information.
# symfony/console
This package relies on symfony/console
, use prepared contributte/console (opens new window) integration.
composer require contributte/console
extensions:
console: Contributte\Console\DI\ConsoleExtension(%consoleMode%)
2
# Configuration
Schema definition
nettrine.fixtures:
paths: <string[]>
2
Under the hood
You should define paths where the fixture classes are stored.
nettrine.fixtures:
paths:
- %appDir%/fixtures
2
3
# Usage
Type bin/console
in your terminal and there should be a doctrine:fixtures
command group.
The doctrine:fixtures:load command loads data fixtures from your configuration by default:
bin/console doctrine:fixtures:load
If you want to append the fixtures instead of first flushing the database you can use the --append option:
bin/console doctrine:fixtures:load --append
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:
bin/console doctrine:fixtures:load --purge-with-truncate
# Fixture
The simplest fixture just implements 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
*/
public function load(ObjectManager $manager): void
{
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
If you need to run the fixtures in a fixed succession, 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
*/
public function load(ObjectManager $manager): void
{
}
/**
* Get the order of this fixture
*/
public function getOrder(): int
{
return 1;
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
If you need to 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
*/
public function load(ObjectManager $manager): void
{
$this->addReference('user', new User());
$this->getReference('user');
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
If you need to use the 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;
public function setContainer(Container $container)
{
$this->container = $container;
}
/**
* Load data fixtures with the passed ObjectManager
*/
public function load(ObjectManager $manager): void
{
$this->container->getService('foo');
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Examples
- https://github.com/contributte/playground (opens new window) (playground)
- https://contributte.org/examples.html (opens new window) (more examples)