okvpn / fixture-bundle
Requires
- php: >=5.5.9
- doctrine/data-fixtures: ^1.0
- doctrine/dbal: ^2.1
- doctrine/orm: ^2.1
- symfony/doctrine-bridge: ~2.7|~3.0
- symfony/framework-bundle: ~2.7|~3.0
Requires (Dev)
- phpunit/phpunit: ^5.7
This package is auto-updated.
Last update: 2024-08-28 04:36:58 UTC
README
简介
Symfony 允许使用数据固定加载数据。但这些固定每次在执行 doctrine:fixtures:load
命令时都会运行。
为了避免多次加载相同的固定,创建了一个 okvpn:fixtures:data:load 命令。此命令保证每个数据固定只加载一次。
此命令支持两种类型的迁移文件:main
数据固定和 demo
数据固定。在安装过程中,用户可以选择是否加载演示数据。
固定顺序可以通过标准的 Doctrine 排序或依赖功能进行更改。有关固定顺序的更多信息,请参阅 doctrine 数据固定手册。
安装
使用 composer 安装
composer require okvpn/fixture-bundle
并将此包添加到您的 AppKernel 中
<?php use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\Config\Loader\LoaderInterface; class AppKernel extends Kernel { public function registerBundles() { $bundles = array( new Okvpn\Bundle\FixtureBundle\OkvpnFixtureBundle(), //... ); } public function registerContainerConfiguration(LoaderInterface $loader) { $loader->load($this->getRootDir().'/config/config_'.$this->getEnvironment().'.yml'); } }
在 config.yml
中配置放置数据固定的目录和表名
okvpn_fixture: table: okvpn_fixture_data path_main: Migrations/Data/ORM path_demo: Migrations/Data/Demo/ORM
基本示例
创建文件 src/Akuma/PassBundle/Migrations/Data/ORM/TestFixture.php
<?php // src/Akuma/PassBundle/Migrations/Data/ORM/TestFixture.php namespace Akuma\PassBundle\Fixture\Data; use Akuma\PassBundle\Entity\Item; use Doctrine\Common\DataFixtures\AbstractFixture; use Doctrine\Common\Persistence\ObjectManager; class TestFixture extends AbstractFixture { public function load(ObjectManager $manager) { $item = new Item(); $item->setHumidity(58.00) ->setTemp(10.8) ->setPressure(1019.23) ->setTimestamp(new \DateTime()); $manager->persist($item); $manager->flush(); } }
然后运行命令 okvpn:fixtures:data:load
以加载它。
版本化固定
有一些固定需要反复执行。例如,上传国家数据的固定。通常,如果您添加新的国家列表,您需要创建一个新的数据固定来上传这些数据。为了避免这种情况,您可以使用版本化数据固定。
要使固定版本化,此固定必须实现 VersionedFixtureInterface 和返回固定数据版本的 getVersion
方法。
示例
<?php namespace Acme\DemoBundle\Migrations\DataFixtures\ORM; use Doctrine\Common\DataFixtures\AbstractFixture; use Doctrine\Common\Persistence\ObjectManager; use Okvpn\Bundle\FixtureBundle\Fixture\VersionedFixtureInterface; class LoadSomeDataFixture extends AbstractFixture implements VersionedFixtureInterface { /** * {@inheritdoc} */ public function getVersion() { return '1.0'; } /** * {@inheritdoc} */ public function load(ObjectManager $manager) { // Here we can use fixture data code which will be run time after time } }
在这个示例中,如果固定尚未加载,它将被加载,并将版本 1.0 保存为当前加载的此固定版本。
为了能够再次加载此固定,固定必须返回大于 1.0 的版本,例如 1.0.1 或 1.1。版本号必须是一个 PHP 标准化的版本号字符串。有关 PHP 标准化的版本号字符串的更多信息,请参阅 PHP 手册。
如果固定需要知道最后加载的版本,则必须实现 LoadedFixtureVersionAwareInterface 和 setLoadedVersion
方法
<?php namespace Acme\DemoBundle\Migrations\DataFixtures\ORM; use Doctrine\Common\DataFixtures\AbstractFixture; use Doctrine\Common\Persistence\ObjectManager; use Okvpn\Bundle\FixtureBundle\Fixture\VersionedFixtureInterface; use Okvpn\Bundle\FixtureBundle\Fixture\RequestVersionFixtureInterface; class LoadSomeDataFixture extends AbstractFixture implements VersionedFixtureInterface, LoadedFixtureVersionAwareInterface { /** * @var $currendDBVersion string */ protected $currendDBVersion = null; /** * {@inheritdoc} */ public function setLoadedVersion($version = null) { $this->currendDBVersion = $version; } /** * {@inheritdoc} */ public function getVersion() { return '2.0'; } /** * {@inheritdoc} */ public function load(ObjectManager $manager) { // Here we can check last loaded version and load data data difference between last // uploaded version and current version } }
许可证
MIT 许可证