api-skeletons / zf-doctrine-data-fixture
Doctrine Data Fixture Console Route with Service Manager configured Fixtures
Installs: 21 540
Dependents: 1
Suggesters: 0
Security: 0
Stars: 10
Watchers: 1
Forks: 70
Open Issues: 1
Requires
- php: ^7.1.0
- doctrine/data-fixtures: ^1.3.0
- doctrine/doctrine-module: ^1.2.0 || ^2.1.0
- zendframework/zend-eventmanager: ^3.0.0
- zendframework/zend-modulemanager: ^2.7.2 || ^3.0.0
- zendframework/zend-servicemanager: ^3.1.0
Requires (Dev)
- api-skeletons/coding-standard: ^1.0.0
- doctrine/doctrine-orm-module: ^1.1.0 || ^2.1
- dprevite/lint: ^1.0.0
- phpstan/phpstan-doctrine: ^0.9.1
- phpunit/phpunit: ^5.7.0
- squizlabs/php_codesniffer: ^3.2.0
- zendframework/zend-i18n: ^2.7.0
- zendframework/zend-test: ^3.0.0
README
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.