evotodi/seed-bundle

symfony 6/7 的 Seeder

安装次数: 36 016

依赖: 0

建议者: 0

安全: 0

星标: 11

关注者: 1

分支: 4

开放问题: 1

类型:symfony-bundle

7.0 2024-01-03 15:41 UTC

This package is auto-updated.

Last update: 2024-09-03 16:58:17 UTC


README

Symfony PhpStorm

Symfony/Doctrine Seed Bundle

用于从 doctrine 数据库或其他需要播种的数据库中加载/卸载数据。

示例:加载包含州列表和缩写的表,或用初始管理员用户填充用户表。与主要用于开发的 DoctrineFixturesBundle 不同,此包用于在初始生产推送之前对数据库进行播种。

安装

使用以下命令安装包

composer require evotodi/seed-bundle

(可选) 将种子作为服务加载到 config/services.yaml
在 Symfony 中,src/ 中的类默认作为服务加载,因此创建一个 src/DataSeeds/ 文件夹将加载种子作为服务。

services:
    Evotodi\SeedBundle\DataSeeds\:
        resource: '../DataSeeds/*'

构建用于填充数据库的种子

Seed 类是一个 Command

  • 必须扩展 Evotodi\SeedBundle\Command\Seed
  • 必须从静态 seedName 方法返回种子名称
  • 种子命名必须遵循 symfony 控制台命令的分号分隔的命名约定。
<?php

namespace App\Seeds;

use Evotodi\SeedBundle\Command\Seed;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

use App\Entity\User;

class UserSeed extends Seed
{
    
    /**
    * Return the name of your seed
    */
    public static function seedName(): string
    {
        /**
        * The seed won't load if this is not set
        * The resulting command will be seed:user
        */
        return 'user';
    }
    
    /**
    * Optional ordering of the seed load/unload.
    * Seeds are loaded/unloaded in ascending order starting from 0. 
    * Multiple seeds with the same order are randomly loaded. 
    */
    public static function getOrder(): int 
    {
      return 0; 
    }
    
    /**
    * The load method is called when loading a seed 
    */
    public function load(InputInterface $input, OutputInterface $output): int
    { 

        /**
        * Doctrine logging eats a lot of memory, this is a wrapper to disable logging
        */ 
        $this->disableDoctrineLogging();

        $users = [
            [
                'email' => 'admin@admin.com',
                'password' => 'password123',
                'roles' => ['ROLE_ADMIN'],
            ],


        ];

        foreach ($users as $user){
            $userRepo = new User();
            $userRepo->setEmail($user['email']);
            $userRepo->setRoles($user['roles']);
            $userRepo->setPassword($this->passwordEncoder->encodePassword($userRepo, $user['password']));
            $this->manager->persist($userRepo);
        }
        $this->manager->flush();
        $this->manager->clear();
        
        /**
        * Must return an exit code.
        * A value other than 0 or Command::SUCCESS is considered a failed seed load/unload.
        */ 
        return 0;
    }
    
    /**
    * The unload method is called when unloading a seed 
    */
    public function unload(InputInterface $input, OutputInterface $output): int
    {
        //Clear the table
        $this->manager->getConnection()->exec('DELETE FROM user');
        
        /**
        * Must return an exit code.
        * A value other than 0 or Command::SUCCESS is considered a failed seed load/unload.
        */ 
        return 0;
    }
}

种子命令

SeedBundle 提供了两个默认命令和您创建的每个种子的一个命令。
使用前面的示例,我会得到

bin/console seed:load
bin/console seed:unload
bin/console seed:user

注意:如果您的种子在 seed: 命令列表中未显示,请清除缓存

全局种子命令

全局的 seed:loadseed:unload 允许您在一个命令中运行多个种子。
本节余下部分将仅显示 seed:load,因为它与 seed:unload 的工作方式相同。

  • seed:load 将按升序加载所有种子。
  • seed:load user 将仅加载用户种子(见上面的示例)。
  • 多个种子如 seed:load user country town 将仅加载这些种子。
  • seed:load --skip country 将加载所有种子,除了 country。允许多个跳过。
  • 带有调试标志的 seed:load --debug 将打印将要加载的内容及其顺序。
  • 带有断言标志的 seed:load --break 将在种子失败时退出种子加载。
  • 带有来源标志的 seed:load --from country 将从 county 种子开始,并从那里加载,跳过较低顺序的值,并可能跳过与 country 相同顺序的值。
    有关更多详细信息,请参阅 全局种子排序

全局种子名称匹配

使用 webmozarts/glob 过滤,通过将种子名称转换为路径样式的字符串来匹配种子名称。
例如,如果您有以下种子

  • prod:users:us
  • prod:users:eu
  • prod:users:it
  • prod:prices
  • prod:products
  • dev:users
  • dev:prices
  • dev:products

并且只想加载 'prod' 组中的用户,则可以调用 seed:load prod/users/*
或者加载整个 'prod' 组,可以调用 seed:load prod/**/*
或者加载 'dev' 价格和产品以及 'prod' 用户,可以调用 seed:load dev:prices dev:products prod:users:*

注意:冒号和斜杠可以互换,因为所有冒号都替换为斜杠进行过滤。

有关 glob 模式的更多信息,请参阅 webmozarts/glob 的说明。

全局种子排序

每个种子都有一个用于排序的 getOrder 方法。默认值是 0
种子按升序加载/卸载。

注意:具有相同顺序值的种子将半随机加载。在使用 --from 选项时,这尤其需要注意。

排序和 --from 的示例问题

种子

  • seed-a 顺序 0
  • seed-b 顺序 1
  • seed-c 顺序 1
  • seed-d 顺序 1
  • seed-e 顺序 2

调用 seed:load --from seed-c 将从 seed-c 开始加载,但由于 seed-b 和 seed-d 具有相同的顺序,一个或两个都可能被加载,具体取决于它们在注册表中的加载顺序。
建议使用 --debug 标志来验证加载顺序或按顺序排列您的种子。

手动种子命令

调用 seed:user load(如上例所示)将只加载用户种子。相反,调用 seed:user unload 将卸载它。

示例项目

在示例文件夹中有一个可以用于实验 Seed 捆绑的项目。
它展示了如何对数据库或平面文件进行种子操作。

感谢

感谢 soyuka/SeedBundle

贡献

非常欢迎贡献!

请创建详细的 issue 和 PR。

许可

此软件包是免费软件,根据 MIT 许可证的条款分发。

更新

  • 2022-05-10
    • 与之前版本的破坏性更改,因为这主要是对捆绑的重新编写。
    • 种子不再需要以 seed 结尾的名称
    • 不再支持在 configure 方法中设置种子名称。请使用静态方法 seedName 返回种子名称。
    • 已删除配置文件
    • 需要 PHP 8+
    • 需要 Symfony 6+
  • 2021-12-06
    • 更新依赖项以允许使用 symfony 5.3 和 5.4
  • 2020-06-03
    • 更新依赖项以允许使用 Symfony 4.4.* 和 5.0.* 和 5.1.*
  • 2020-04-23
    • 更新依赖项以允许使用 Symfony 4.4.* 和 5.0.*
    • 为加载和卸载函数添加了必需的返回退出代码
    • 更新测试以反映必需的返回代码