php-7.4 - 从迁移文件运行 Phinx 种子

标签 php-7.4 phinx

我正在编写一组 Phinx 迁移,用于安装应用程序数据库并插入种子。我的前 4 个迁移文件分别构建了数据库架构(带有外键约束)、触发器、函数和存储过程。我想使用第五个迁移文件来执行所有种子,以便此后的每个迁移文件都可以使用种子数据。

我想要一个从 Phinx 迁移文件的 up 方法内部运行所有应用程序种子的示例。

最佳答案

从迁移文件执行播种器:

public function change()
{
    $table = $this->table('migration_test');
    $table->addColumn('example', 'string', ['limit' => 10]);
    $table->create();

    exec('/usr/local/bin/php ./vendor/bin/phinx seed:run --seed=MySeeder');
}

从迁移文件运行播种器的另一种方法:

<?php
declare(strict_types=1);
$namespaceDefinition
use $useClassName;

require_once __DIR__ . '/../seeds/SeederName.php';

final class $className extends $baseClassName
{
    public function up()
    {
        (new SeederName())
            ->setAdapter($this->getAdapter())
            ->setInput($this->getInput())
            ->setOutput($this->getOutput())
            ->run();
    }

    public function down()
    {
        // probably truncate
    }
}

关于php-7.4 - 从迁移文件运行 Phinx 种子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70025190/

相关文章:

operator-keyword - (??=) 双问号和等号,那个运算符有什么作用?

php - 如何在 DSN 中指定用户名和密码?

eloquent - 第二次运行 Phinx migrate 不会用新的迁移更新数据库

cakephp-3.0 - CakePHP 3 迁移缺少枚举

php - 在 php 7.4 中重写匿名函数

php - PHP 7.4 中的循环引用

php - 无法在 CentOS 7 上安装 phpMyAdmin

php - Phinx迁移SQLSTATE[42S01] : Base table or view already exists:

php - 在 CakePHP 3 中动态动态添加现有表中的列