Database Seeding
Note
Database seeding is entirely optional, and Phinx does not create a seeds
directory by default.
Phinx includes a command to easily generate a new seed class:
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:
- <?php
- use Phinx\Seed\AbstractSeed;
- class MyNewSeeder extends AbstractSeed
- {
- /**
- * Run Method.
- *
- * Write your database seeder using this method.
- *
- * More information on writing seeders is available here:
- * http://docs.phinx.org/en/latest/seeding.html
- */
- public function run()
- {
- }
- }
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.
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.
Seed classes can also use the familiar Table
object to insert data. You canretrieve an instance of the Table object by calling the method fromwithin your seed class and then use the insert()
method to insert data:
- <?php
- use Phinx\Seed\AbstractSeed;
- class PostsSeeder extends AbstractSeed
- {
- public function run()
- {
- [
- 'body' => 'foo',
- 'created' => date('Y-m-d H:i:s'),
- ],
- [
- 'body' => 'bar',
- 'created' => date('Y-m-d H:i:s'),
- ]
- ];
- $posts = $this->table('posts');
- $posts->insert($data)
- ->save();
- }
- }
Note
You must call the save()
method to commit your data to the table. Phinxwill buffer data until you do so.
It’s trivial to use the awesomeFaker library in your seed classes.Simply install it using Composer:
- <?php
- use Phinx\Seed\AbstractSeed;
- class UserSeeder extends AbstractSeed
- {
- public function run()
- {
- $faker = Faker\Factory::create();
- $data = [];
- for ($i = 0; $i < 100; $i++) {
- $data[] = [
- 'username' => $faker->userName,
- 'password' => sha1($faker->password),
- 'password_salt' => sha1('foo'),
- 'email' => $faker->email,
- 'first_name' => $faker->firstName,
- 'last_name' => $faker->lastName,
- 'created' => date('Y-m-d H:i:s'),
- }
- $this->insert('users', $data);
- }
- }
In addition to inserting data Phinx makes it trivial to empty your tables usingthe SQL TRUNCATE
command:
- <?php
- use Phinx\Seed\AbstractSeed;
- class UserSeeder extends AbstractSeed
- {
- public function run()
- {
- $data = [
- [
- 'body' => 'foo',
- 'created' => date('Y-m-d H:i:s'),
- ],
- [
- 'body' => 'bar',
- 'created' => date('Y-m-d H:i:s'),
- ]
- ];
- $posts = $this->table('posts');
- $posts->insert($data)
- ->save();
- // empty the table
- $posts->truncate();
- }
- }
Note
SQLite doesn’t natively support the 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.
This is the easy part. To seed your database, simply use the seed:run
command:
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 parameter:
- $ php vendor/bin/phinx seed:run -s UserSeeder
You can also run multiple seeders:
- $ php vendor/bin/phinx seed:run -s UserSeeder -s PermissionSeeder -s LogSeeder
The Phinx seed functionality provides a simple mechanism to easily and repeatablyinsert test data into your database.