ticketpark / doctrine-fixtures-autoloader
Simplifies loading doctrine fixtures
Installs: 76 316
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 1
Requires
- php: >=5.3.3
- doctrine/data-fixtures: ^1.1
Requires (Dev)
- phpunit/phpunit: ^4.0|^5.0
This package is not auto-updated.
Last update: 2024-12-17 20:51:11 UTC
README
This library simplifies loading Doctrine fixtures.
Installation
Add TicketparkDoctrineFixturesAutoloader to your composer.json:
{ "require": { "ticketpark/doctrine-fixtures-autoloader" } }
Usage
<?php namespace Acme\Bundle\SomeBundle\DataFixtures\ORM; use Doctrine\Common\DataFixtures\FixtureInterface; use Doctrine\Common\Persistence\ObjectManager; use Ticketpark\Doctrine\DataFixtures\Autoloader\Autoloader; class LoadCountryData extends AutoLoader implements FixtureInterface { public function load(ObjectManager $manager) { $data = array( array( '_reference' => 'CH', 'shortCode' => 'CH', 'name' => 'Switzerland' ), array( '_reference' => 'AT', 'shortCode' => 'AT', 'name' => 'Austria' ), ); $this->autoload($data, $manager); } }
In a second fixture class, references will be available based on the entity name and the optional _reference
value:
<?php namespace Acme\Bundle\SomeBundle\DataFixtures\ORM; use Doctrine\Common\DataFixtures\FixtureInterface; use Doctrine\Common\DataFixtures\DependentFixtureInterface; use Doctrine\Common\Persistence\ObjectManager; use Ticketpark\Doctrine\DataFixtures\Autoloader\Autoloader; class LoadUserData extends AutoLoader implements FixtureInterface, DependentFixtureInterface { public function getDependencies() { return array( 'Acme\Bundle\SomeBundle\DataFixtures\ORM\LoadCountryData' ); } public function load(ObjectManager $manager) { $data = array( array( // The string `country_CH` references the element // created in the 'Country' entity with 'CH' as its // _reference value. 'country' => $this->getReference('country_CH'), 'name' => 'Tom Swissman' ) ); $this->autoload($data, $manager); } }
Overwriting setter method names
In some cases you might want to override the setter methods. for instance because your method is named addCurrency
instead of addCurrencie
as the autoloader by default would asume. In this case, simply use the additional setterMethods
parameter:
<?php namespace Acme\Bundle\SomeBundle\DataFixtures\ORM; use Doctrine\Common\DataFixtures\FixtureInterface; use Doctrine\Common\Persistence\ObjectManager; use Ticketpark\Doctrine\DataFixtures\Autoloader\Autoloader; class LoadCurrencyData extends AutoLoader implements FixtureInterface { public function load(ObjectManager $manager) { $data = array( array( 'currencies' => array( 'USD', 'EUR', 'CHF' ) ), ); $setterMethods = array( 'currencies' => 'addCurrency' ); $this->autoload($data, $manager, $setterMethods); } }
Treat arrays as singles
Another option to treat an array like a single element and inject the full array into a setter is by using the $treatAsSingle
method parameter.
<?php namespace Acme\Bundle\SomeBundle\DataFixtures\ORM; use Doctrine\Common\DataFixtures\FixtureInterface; use Doctrine\Common\Persistence\ObjectManager; use Ticketpark\Doctrine\DataFixtures\Autoloader\Autoloader; class LoadCurrencyData extends AutoLoader implements FixtureInterface { public function load(ObjectManager $manager) { $data = array( array( 'currencies' => array( 'USD', 'EUR', 'CHF' ) ), ); // this will cause a call to setCurrencies() with the full currencies array $treatAsSingles = array('currencies'); $this->autoload($data, $manager, array(), $treatAsSingles); } }
Provide class name
By default, the library tries to guess your entity namespace based on standard data fixture naming conventions. However, you can also define the namespace of your entity manually:
<?php namespace Acme\Bundle\SomeBundle\DataFixtures\ORM; use Doctrine\Common\DataFixtures\FixtureInterface; use Doctrine\Common\Persistence\ObjectManager; use Ticketpark\Doctrine\DataFixtures\Autoloader\Autoloader; class LoadCountryData extends AutoLoader implements FixtureInterface { public function load(ObjectManager $manager) { $data = array( array( 'shortCode' => 'CH', 'name' => 'Switzerland' ), ); $this->setEntityClass('My\Custom\Namespace\Country'); $this->autoload($data, $manager); } }
License
This bundle is under the MIT license. See the complete license in the bundle:
Resources/meta/LICENSE