Phinx: Ordering for database seeding

Created on 19 May 2017  路  5Comments  路  Source: cakephp/phinx

Currently there is no way of saying that one file needs to be run before another in the files themselves. It would be nice to be able to have a timestamp associated with a seed file name or some sort of command to make sure that certain files are run before others so that migrations can be run in a specific order. As the functionality exists right now you have to change the name of the seeder based on the order you want it to run based on foreign key constraints. Migrations don't have this issue because the timestamp at the beginning of the file help to specify the order the files get executed in. This means that for migrations if there is a foreign key that it is guaranteed the table exists before hand. However, with seeding that is not guaranteed and the data does not necessarily get inserted into the database in order.

Most helpful comment

For anyone still struggling with this, here is what I did (that required little work);

  1. Create a seeder, in its run() method, loop through the other seeder classes and trigger their run() method. See code snippet below:
<?php

use Phinx\Seed\AbstractSeed;

class MainSeeder extends AbstractSeed
{
    // the classes in the $seederClasses should be ordered the way you want them executed
    protected $seedClasses = [
        UserTypeSeeder::class,
        UserSeeder::class,
    ];

    public function run()
    {
        foreach ($this->seedClasses as $seedClass) {
            /** @var AbstractSeed $seeder */
            $seeder = new $seedClass;
            $seeder->setAdapter($this->getAdapter()); // this is required to set the database connection
            $seeder->run();
        }
    }
}
  1. In your terminal run the command phinx seed:run -s MainSeeder

  2. You're done.

All 5 comments

I agree. Errors may occur based on the loading order of the seed files. Curious to know how others solve this when loading all their seed files in bulk. Are you designating the order of the files alphabetically, or do you just manually load each seed file one by one, paying attention to the order as you go?

I've created a bash script bin\db-reset.sh to do a rollback and run seeds in defined order

./phinx rollback -t 0
./phinx seed:run -s ClientsSeeder -s EntityTypesSeeder
./phinx seed:run -s MockValuesSeeder

I was considering other solutions, but this one makes most sense to me

Hey, I created pull request for seeds ordering feature:
https://github.com/cakephp/phinx/pull/1244

Please review my pull request and comment if you like it.

For anyone still struggling with this, here is what I did (that required little work);

  1. Create a seeder, in its run() method, loop through the other seeder classes and trigger their run() method. See code snippet below:
<?php

use Phinx\Seed\AbstractSeed;

class MainSeeder extends AbstractSeed
{
    // the classes in the $seederClasses should be ordered the way you want them executed
    protected $seedClasses = [
        UserTypeSeeder::class,
        UserSeeder::class,
    ];

    public function run()
    {
        foreach ($this->seedClasses as $seedClass) {
            /** @var AbstractSeed $seeder */
            $seeder = new $seedClass;
            $seeder->setAdapter($this->getAdapter()); // this is required to set the database connection
            $seeder->run();
        }
    }
}
  1. In your terminal run the command phinx seed:run -s MainSeeder

  2. You're done.

This shows up in search results. Thanks to cakephp/phinx#1244, you can now use getDependencies(). Here's how: https://github.com/cakephp/phinx/blob/a96c5465a8f3dd64e39c9115a5d383b8d01928ce/tests/Phinx/Migration/_files/seeds/PostSeeder.php

Was this page helpful?
0 / 5 - 0 ratings

Related issues

l0gicgate picture l0gicgate  路  16Comments

hmp5bs picture hmp5bs  路  17Comments

orderbynull picture orderbynull  路  19Comments

aimfeld picture aimfeld  路  23Comments

jeremylivingston picture jeremylivingston  路  20Comments