# Nettrine Fixtures

Doctrine/DataFixtures for Nette Framework

# Content

# Setup

Install package

composer require nettrine/fixtures
1

Register extension

extensions:
  nettrine.fixtures: Nettrine\Fixtures\DI\FixturesExtension
1
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 integration. Doctrine ORM depends on Doctrine DBAL, use prepared nettrine/dbal integration

composer require nettrine/dbal
composer require nettrine/orm
1
2

Without these packages you can't process fixtures, because fixtures needs connection to database and information about entities.

# symfony/console

This package relies on symfony/console, use prepared contributte/console integration.

composer require contributte/console
1
extensions:
  console: Contributte\Console\DI\ConsoleExtension(%consoleMode%)
1
2

# Configuration

Schema definition

nettrine.fixtures:
  paths: <string[]>
1
2

Under the hood

You should define paths where the fixture classes are stored.

nettrine.fixtures:
  paths:
    - %appDir%/fixtures
1
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
1

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

bin/console 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:

bin/console 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:

bin/console doctrine:fixtures:load --purge-with-truncate
1

Console Commands

# 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
   */
  public function load(ObjectManager $manager): void
  {
  }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

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
   */
  public function load(ObjectManager $manager): void
  {
  }

  /**
   * Get the order of this fixture
   */
  public function getOrder(): int
  {
    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

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
   */
  public function load(ObjectManager $manager): void
  {
    $this->addReference('user', new User());
    $this->getReference('user');
  }

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

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;

  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');
  }

}
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

# Examples

You can find more examples in planette playground repository.