intechventures/seed-bundle

此包已被废弃,不再维护。没有建议的替代包。

通过 symfony 命令实现种子助手

2.0.10 2021-05-10 12:16 UTC

This package is auto-updated.

Last update: 2023-01-10 16:11:25 UTC


README

描述

加载类似于 Laravel 种子的种子数据,非常适合创建需要持久存储在数据库中的测试数据。

可以用于此的唯一 symfony 扩展是 DoctrineFixturesBundle,但这些是固定数据,应仅用于测试,因为这些数据不会持久保存在数据库中。

通过 卸载 数据,此包也可以用作固定数据包。

安装

composer require intechventures/seed-bundle

配置

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:loadseed: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.