2

I'm writing a set of Phinx migrations that install an application database and insert the seeds. My first 4 migration files build the database schema (with foreign key constraints), triggers, functions, and stored procedures respectively. I'd like to use a 5th migration file to execute all of the seeds so that every migration file thereafter will have seed data to work with.

I'd like an example of running all of the application seeds FROM INSIDE the up method of a Phinx migration file.

Tanoro
  • 871
  • 2
  • 10
  • 30

1 Answers1

2

Executing seeder from migration file :

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');
}

Another way for running seeder from migration file :

<?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
    }
}
Shinoy p b
  • 353
  • 3
  • 13