Seeders are way to put data immediately into your tables. All you need to do is populate an array, then use that array to create rows in your table. You can use this data as a test, or to put in actual data without having to type it in multiple times or enter it in a form.
Create A Seeder
Creating a Seeder is simple using bake.
$ bin\cake bake seed Users
This will create your UsersSeed.php
file in the /config/Seeds
directory.
Insert your data in the $data
array using the 'field_name' => 'value'
convention. Be warned, your seeder will not utilize your Entity Model if you've created it, so if you have any automated fields (like a slug created on beforeSave()
) or mutators (like _setPassword()
), they won't work unless you recreate them in your seeder.
<?php
declare(strict_types=1);
use Cake\Utility\Text;
use Migrations\AbstractSeed;
use Authentication\PasswordHasher\DefaultPasswordHasher;
class UsersSeed extends AbstractSeed
{
public function run(): void
{
$hasher = new DefaultPasswordHasher();
$data = [
[
'username' => 'naidim',
'password' => $hasher->hash('password'),
'email' => 'naidim@gmail.com',
'first_name' => 'Charles',
'last_name' => 'Patterson',
'slug' => mb_strtolower(Text::slug('Charles Patterson')),
'role' => 'Admin',
],
];
$table = $this->table('users');
$table->insert($data)->save();
}
}
Get Dependencies
If your Seeder requires a reference to any other table, you must use the getDependencies()
method to ensure that Seeder runs first. For example, Phone Numbers have a foreign Key user_id
referencing the Users table. You need to have the UsersSeed run first so the id's inserted with PhoneNumbersSeed are valid.
class PhoneNumbersSeed extends AbstractSeed
{
public function getDependencies(): array
{
return [
'UsersSeed'
];
}
...
}
Run Your Seeders
Use the migrations command to run your seeders and insert the data.
$ bin\cake migrations seed
Specify a Seeder
If you have multiple seeders, Cake will run them all unless you specify which seeder you want, and if there are any unique fields you'll get a Duplicate entry error.
$ bin\cake migrations seed --seed UsersSeed
Add A Truncate
Another way to avoid issues running your seeders is to add a truncate()
call before adding your data, so it always starts with an empty table. However, this will not work if there is a foreign key constraint on the table.
public function run(): void
{
...
$table = $this->table('users');
$table->truncate();
$table->insert($data)->save();
}
Exporting Existing Data
If you have a table with already existing data you can export it to a seed file using the --data
flag.
$ bin/cake bake seed --data PhoneNumbers
This extraction of data does not know the relationship between tables, so if there are any dependencies, such as Users for the PhoneNumbers table, you'll have to add them to your extracted seed file manually.
Create a Phone Number
Practice what you've learned by adding a phone number to your Phone Number seeder in /config/Seeds/PhoneNumberSeed.php
and insert the data.