Skip to content

Latest commit

 

History

History

.docs

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Contributte Doctrine Fixtures

Integration of Doctrine DataFixtures for Nette Framework.

Content

Installation

Install package using composer.

composer require nettrine/annotations

Register prepared compiler extension in your config.neon file.

extensions:
  nettrine.fixtures: Nettrine\Fixtures\DI\FixturesExtension

Note

This is just Fixtures, for ORM use nettrine/orm or DBAL use nettrine/dbal.

Configuration

Minimal configuration

nettrine.fixtures:
  paths:
    - %appDir%/fixtures

Advanced configuration

Here is the list of all available options with their types.

nettrine.fixtures:
  paths: <string[]>

Usage

Console

Type bin/console in your terminal and there should be a doctrine:fixtures command group.

bin/console doctrine:fixtures:load
bin/console doctrine:fixtures:load --fixtures=db/fixtures/development

By default, the fixtures are appended to the database. If you want to delete all data before loading fixtures, use --purge option.

bin/console doctrine:fixtures:load --purge=truncate
bin/console doctrine:fixtures:load --purge=delete

Console Commands

Fixtures

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
  {
  }

}

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

}

If you need to run the fixtures in a fixed order after some other fixture, implement Doctrine\Common\DataFixtures\DependentFixtureInterface

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

class Foo2Fixture implements FixtureInterface, DependentFixtureInterface
{

  /**
   * Load data fixtures with the passed ObjectManager
   */
  public function load(ObjectManager $manager): void
  {
  }

  /**
   * Get the order of this fixture
   */
  public function getDependencies(): int
  {
    return [Foo1Fixture::class];
  }

}

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

}

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

}

Services

To autoload your fixtures, register them as services in your config.neon file.

services:
  - App\Fixtures\Foo1Fixture
  - App\Fixtures\Foo2Fixture
  - App\Fixtures\Foo3Fixture
  - App\Fixtures\Foo4Fixture

Examples

Tip

Take a look at more examples in contributte/doctrine.