Database Seeding

In version 0.5.0 Phinx introduced support for seeding your database with testdata. Seed classes are a great way to easily fill your database with data afterit’s created. By default they are stored in the seeds directory; however,this path can be changed in your configuration file.

Note

Database seeding is entirely optional, and Phinx does not create a seedsdirectory by default.

Creating a New Seed Class

Phinx includes a command to easily generate a new seed class:

  1. $ php vendor/bin/phinx seed:create UserSeeder

If you have specified multiple seed paths, you will be asked to select whichpath to create the new seed class in.

It is based on a skeleton template:

  1. <?php
  2.  
  3. use Phinx\Seed\AbstractSeed;
  4.  
  5. class MyNewSeeder extends AbstractSeed
  6. {
  7. /**
  8. * Run Method.
  9. *
  10. * Write your database seeder using this method.
  11. *
  12. * More information on writing seeders is available here:
  13. * http://docs.phinx.org/en/latest/seeding.html
  14. */
  15. public function run()
  16. {
  17.  
  18. }
  19. }

The AbstractSeed Class

All Phinx seeds extend from the AbstractSeed class. This class provides thenecessary support to create your seed classes. Seed classes are primarily usedto insert test data.

The Run Method

The run method is automatically invoked by Phinx when you execute the _seed:run_command. You should use this method to insert your test data.

Note

Unlike with migrations, Phinx does not keep track of which seed classes havebeen run. This means database seeders can be run repeatedly. Keep this inmind when developing them.

Inserting Data

Using The Table Object

Seed classes can also use the familiar Table object to insert data. You canretrieve an instance of the Table object by calling the table() method fromwithin your seed class and then use the insert() method to insert data:

  1. <?php
  2. use Phinx\Seed\AbstractSeed;
  3.  
  4. class PostsSeeder extends AbstractSeed
  5. {
  6. public function run()
  7. {
  8. $data = [
  9. [
  10. 'body' => 'foo',
  11. 'created' => date('Y-m-d H:i:s'),
  12. ],
  13. [
  14. 'body' => 'bar',
  15. 'created' => date('Y-m-d H:i:s'),
  16. ]
  17. ];
  18.  
  19. $posts = $this->table('posts');
  20. $posts->insert($data)
  21. ->save();
  22. }
  23. }

Note

You must call the save() method to commit your data to the table. Phinxwill buffer data until you do so.

Integrating with the Faker library

It’s trivial to use the awesomeFaker library in your seed classes.Simply install it using Composer:

  1. $ composer require fzaninotto/faker

Then use it in your seed classes:

  1. <?php
  2.  
  3. use Phinx\Seed\AbstractSeed;
  4.  
  5. class UserSeeder extends AbstractSeed
  6. {
  7. public function run()
  8. {
  9. $faker = Faker\Factory::create();
  10. $data = [];
  11. for ($i = 0; $i < 100; $i++) {
  12. $data[] = [
  13. 'username' => $faker->userName,
  14. 'password' => sha1($faker->password),
  15. 'password_salt' => sha1('foo'),
  16. 'email' => $faker->email,
  17. 'first_name' => $faker->firstName,
  18. 'last_name' => $faker->lastName,
  19. 'created' => date('Y-m-d H:i:s'),
  20. ];
  21. }
  22.  
  23. $this->insert('users', $data);
  24. }
  25. }

Truncating Tables

In addition to inserting data Phinx makes it trivial to empty your tables usingthe SQL TRUNCATE command:

  1. <?php
  2.  
  3. use Phinx\Seed\AbstractSeed;
  4.  
  5. class UserSeeder extends AbstractSeed
  6. {
  7. public function run()
  8. {
  9. $data = [
  10. [
  11. 'body' => 'foo',
  12. 'created' => date('Y-m-d H:i:s'),
  13. ],
  14. [
  15. 'body' => 'bar',
  16. 'created' => date('Y-m-d H:i:s'),
  17. ]
  18. ];
  19.  
  20. $posts = $this->table('posts');
  21. $posts->insert($data)
  22. ->save();
  23.  
  24. // empty the table
  25. $posts->truncate();
  26. }
  27. }

Note

SQLite doesn’t natively support the TRUNCATE command so behind the scenesDELETE FROM is used. It is recommended to call the VACUUM commandafter truncating a table. Phinx does not do this automatically.

Executing Seed Classes

This is the easy part. To seed your database, simply use the seed:run command:

  1. $ php vendor/bin/phinx seed:run

By default, Phinx will execute all available seed classes. If you would like torun a specific class, simply pass in the name of it using the -s parameter:

  1. $ php vendor/bin/phinx seed:run -s UserSeeder

You can also run multiple seeders:

  1. $ php vendor/bin/phinx seed:run -s UserSeeder -s PermissionSeeder -s LogSeeder

You can also use the -v parameter for more output verbosity:

  1. $ php vendor/bin/phinx seed:run -v

The Phinx seed functionality provides a simple mechanism to easily and repeatablyinsert test data into your database.