我正在编写一组 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/