ticketpark / fixtures-autoload-bundle
1.0
2015-12-07 12:05 UTC
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: 2022-02-01 12:38:09 UTC
README
这个库简化了加载 Doctrine 固定数据的过程。
安装
将 TicketparkDoctrineFixturesAutoloader 添加到你的 composer.json 文件中
{ "require": { "ticketpark/doctrine-fixtures-autoloader" } }
使用方法
<?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); } }
在第二个固定类中,根据实体名称和可选的 _reference
值,将提供引用
<?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); } }
覆盖设置器方法名称
在某些情况下,您可能想覆盖设置器方法。例如,因为您的名称是 addCurrency
而不是 addCurrencie
,而自动加载器默认会假定。在这种情况下,只需使用额外的 setterMethods
参数即可。
<?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); } }
将数组视为单个元素
通过使用 $treatAsSingle
方法参数,您还可以将数组视为单个元素,并将整个数组注入到设置器中。
<?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); } }
提供类名
默认情况下,库会根据标准数据固定命名约定尝试猜测您的实体命名空间。但是,您也可以手动定义实体命名空间。
<?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); } }
许可证
此包采用 MIT 许可证。请参阅包中的完整许可证。
Resources/meta/LICENSE