Doctrine Data Fixture Console Route with Service Manager configured Fixtures
Clone or download
Pull request Compare This branch is 110 commits ahead of Hounddog:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
test
.gitignore
.travis.yml
LICENSE
README.md
composer.json
phpcs.xml.dist
phpstan.neon
phpunit.xml

README.md

ZF Doctrine Data Fixture

Build status Gitter Patreon Total Downloads

This provides command line support for Doctrine Fixtures in Zend Framework 2. Often projects will have multiple sets of fixtures for different object managers or modules such as from a 3rd party API. When this is the case a tool which can run fixtures in groups is needed. Additionally dependency injection must be available to the fixtures. To accomplish these needs this module uses a Zend Service Manager configurable on a per-group basis.

Releases

The 4.x release tags support PHP 5.6 and 7.0.

The 5.x release tags support PHP 7.1 and above.

Installation

Installation of this module uses composer. For composer documentation, please refer to getcomposer.org.

$ composer require api-skeletons/zf-doctrine-data-fixture ^2.0

Add this module to your application's configuration:

'modules' => [
   ...
   'ZF\Doctrine\DataFixture',
],

zf-component-installer

If you use zf-component-installer, that plugin will install zf-doctrine-data-fixture as a module for you.

Configuration

This module builds on top of Doctrine configuration. The configuration in a module which implements fixtures is:

return [
    'doctrine' => [
        'fixture' => [
            'group1' => [
                'object_manager' => 'doctrine.entitymanager.orm_default',
                'invokables' => [
                    'ModuleName\Fixture\FixtureOne' => 'ModuleName\Fixture\FixtureOne',
                ],
                'factories' => [
                    'ModuleName\Fixture\FixtureTwo' => 'ModuleName\Fixture\FixtureTwoFactory',
                ]
            ],
            'group2' => [
                'object_manager' => 'doctrine.entitymanager.orm_zf_doctrine_audit',
                ...
            ],
        ],
    ],
];

Each group is a Zend ServiceManager configuration. This allows complete dependency injection control of your fixtures.

Listing Fixtures

index.php data-fixture:list [<group>]

List all object managers and their groups, list all groups for a given object manager, or specify object manager and group to list all fixtures for a group.

Executing Fixtures from Command Line

index.php data-fixture:import <group> [--purge-with-truncate] [--do-not-append]

The <group> is required.

The default was to not append and this was constantly overridden with --append. Append is now to the default option. This is inversed with the new --do-not-append

Options:

--purge-with-truncate if specified will purge the object manager's tables before running fixtures.

--do-not-append will delete all data in the database before running fixtures.

Executing Fixtures from Code

For unit testing or other times you must run your fixtures from within code you must fetch the DataFixtureManager with build and pass the group name to the service manager then load the fixtures into the Loader manually.

use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use ZF\Doctrine\DataFixture\Loader;

// Run audit fixtures
$dataFixtureManager = $application->getServiceManager()
    ->build('ZF\Doctrine\DataFixture\DataFixtureManager', ['group' => 'zf-doctrine-audit']);

$loader = new Loader($dataFixtureManager);
$purger = new ORMPurger();
$executor = new ORMExecutor($auditEntityManager, $purger);

foreach ($dataFixtureManager->getAll() as $fixture) {
    $loader->addFixture($fixture);
}

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

Getting Help

index.php data-fixture:help

Important Notes

  • You can only run one group at a time from the command line. If you need to run more create a script to run them in sequence.
  • The ServiceManager is injected into each DataFixtureManager at getServiceLocator() so you can use instantiators which run from that level. This makes the DataFixtureManager work like a plugin manager defined with $serviceListener->addServiceManager().
  • You cannot use abstract factories. Each fixture must be individually configured.

History

Version 1.0 of this module grouped groups by partial object manager alias. This limited grouping of 3rd party fixtures and the flexibility of using ODM.

This module is a near complete rewrite of hounddog/doctrine-data-fixture-module. All the patterns have changed and the code was reduced. That module served me and the community well for a long time.