ds-restauration / seed-bundle
通过 symfony 命令进行种子助手
3.0.0
2019-07-08 15:56 UTC
Requires
- php: >=7.0.0
- doctrine/doctrine-bundle: ~1.9.1
- doctrine/orm: ~2.6.2
- symfony/console: ~4.2.0
- symfony/finder: ~4.2.0
- symfony/framework-bundle: ~4.2.0
Requires (Dev)
- phpunit/phpunit: ~6.5.12
README
描述
生成并持久化种子数据。源于 Soyuka 的种子包:https://github.com/soyuka/SeedBundle
配置
ds_restauration_seed: prefix: 'seed' # Command prefix "seed:yourseedname" directory: 'Seeds' # Default seed path: Bundle/Seeds separator: ':' seed_order: # Order to load seeds in, lowest loads first. Any seed not in the list defaults to an order of 0 seed1: -1 seed2: 1 seed_bundle: # Order to load bundles in, lowest loads first. Any bundles not in the list defaults to an order of 0 bundle1: -1 bundle2: 1
构建种子
Seed 类是一个 Command,并且
- 必须扩展
DsRestauration\SeedBundle\Command\Seed - 类名必须以
Seed结尾 - 必须在配置方法中调用
setSeedName
<?php namespace AcmeBundle\ISOBundle\Seeds; use DsRestauration\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'); //Needed if you want to load seeds by bundle $this->setBundleName('geo'); 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(); } }
加载种子
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。
种子顺序
种子顺序在配置文件中定义。顺序最低的种子先加载。任何未定义顺序的种子默认为顺序 0(将首先加载)。
包顺序
与种子顺序类似,包顺序也在配置文件中定义。顺序最低的包先加载。任何未定义顺序的包默认为顺序 0(将首先加载)。
按包/加载种子
可以使用 seed:load 的 --bundle 或 -bn 选项完成(seed:load --bundle geo --bundle params)。在这个例子中,只有那些将包定义为 'geo' 或 'params' 的种子会被加载。
仅加载特定种子
可以使用 seed:load 的 --seed 或 -s 选项完成(seed:load --seed countries --seed regions)。在这个例子中,只有那些将种子定义为 'countries' 或 'regions' 的种子会被加载。
跳过种子
可以使用 '--skip' 参数跳过一个或多个种子
跳过包
可以使用 '--skip-bundle' 参数跳过一个或多个包
许可证
The MIT License (MIT)
Copyright (c) 2019 Ds Restauration
Copyright for the original project (Soyuka\SeedBundle) is held by Antoine Bluchet, 2015
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.