soyuka / seed-bundle
此包已被废弃且不再维护。未建议替换包。
通过symfony命令的种子助手
2.0.7
2017-12-04 13:32 UTC
Requires
- php: >=7.0.0
- doctrine/doctrine-bundle: ~1.6
- doctrine/orm: ~2.5
- symfony/console: ^3.0
- symfony/finder: ^3.0
- symfony/framework-bundle: ^3.0
Requires (Dev)
- phpunit/phpunit: ^5.2
README
/!\ 从2018年1月1日起,我将不再积极维护此项目。如果有任何PR,我会合并它们,但我不再使用它了 /!\
原因
我需要一些可以保持数据库中的种子数据。唯一可用于此目的的symfony包是DoctrineFixturesBundle。这些是固定数据,应仅用于测试。当命令运行时,包含在此的数据将被删除/清除。这里,我希望数据是持久的。您也可以通过unloading
数据将其用作固定数据包,但这不是其主要目的。
配置
soyuka_seed: prefix: 'seed' #command prefix "seed:yourseedname" directory: 'Seeds' #default seed path: Bundle/Seeds separator: ':'
构建种子
Seed
类是一个Command
- 必须扩展
Soyuka\SeedBundle\Command\Seed
- 类名必须以
Seed
结尾 - 必须在配置方法中调用
setSeedName
<?php namespace AcmeBundle\ISOBundle\Seeds; use Soyuka\SeedBundle\Command\Seed; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Yaml\Parser; use AcmeBundle\ISOBundle\Entity\Country; class CountrySeed extends Seed { protected function configure() { //The seed won't load if this is not set //The resulting command will be {prefix}:country $this ->setSeedName('country'); parent::configure(); } public function load(InputInterface $input, OutputInterface $output){ //Doctrine logging eats a lot of memory, this is a wrapper to disable logging $this->disableDoctrineLogging(); //Access doctrine through $this->doctrine $countryRepository = $this->doctrine->getRepository('AcmeISOBundle:Country'); $yaml = new Parser(); //for example, using umpirsky/country-list (lazy yaml) $countries = $yaml->parse(file_get_contents('vendor/umpirsky/country-list/country/cldr/fr/country.yaml')); foreach ($countries as $id => $country) { if($countryRepository->findOneById($id)) { continue; } $e = new Country(); $e->setId($id); $e->setName($country); //Doctrine manager is also available $this->manager->persist($e); $this->manager->flush(); } $this->manager->clear(); } public function getOrder() { return 0; } }
加载种子
SeedBundle为你提供了两个默认命令和每个你创建的种子的一个命令。在之前的例子中,我会得到
app/console seed:load #calls the load method of every seed
app/console seed:unload #calls the unload method of every seed
app/console seed:country
全局的seed:load
和seed:unload
允许你在单个命令中运行多个种子。当然,你可以跳过种子app/console seed:load --skip Town
,也可以指定你想要的种子app/console seed:load Country
。有关更多信息,请使用app/console seed:load --help
。
种子顺序
每个种子都有一个getOrder
方法,用于对其进行排序。默认值为0
。
许可
The MIT License (MIT)
Copyright (c) 2015 Antoine Bluchet
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.