Database seeding is a simple way to add data into your database. It is especially useful during development where you need to populate the database with sample data that you can develop against, but it is not limited to that. Seeders can contain static data that you don't want to include in a migration, like countries, or geo-coding tables, event or setting information, and more.
Database seeders are simple classes that must have a run() method, and extend CodeIgniter\Database\Seeder.
Within the run() the class can create any form of data that it needs to. It has access to the database
connection and the forge through $this->db and $this->forge, respectively. Seed files must be
stored within the app/Database/Seeds directory. The name of the file must match the name of the class.
.. literalinclude:: seeds/001.php
Seeders can call other seeders, with the call() method. This allows you to easily organize a central seeder,
but organize the tasks into separate seeder files:
.. literalinclude:: seeds/002.php
You can also use a fully-qualified class name in the call() method, allowing you to keep your seeders
anywhere the autoloader can find them. This is great for more modular code bases:
.. literalinclude:: seeds/003.php
You can grab a copy of the main seeder through the database config class:
.. literalinclude:: seeds/004.php
You can specify a different database group when obtaining a seeder instance by passing the group name as the first parameter:
.. literalinclude:: seeds/005.php
When using call() to run child seeders, the database connection is automatically passed to them.
This means child seeders will use the same connection as the parent seeder, unless they explicitly
specify their own $DBGroup property.
If a seeder needs to always use a specific database group regardless of the parent seeder's connection,
you can set the $DBGroup property in the seeder class:
.. literalinclude:: seeds/006.php
The connection priority is:
- If
$DBGroupis set in the seeder class, that connection group is always used - Otherwise, if a connection was passed (from parent seeder via
call()or fromDatabase::seeder()), it is used - Otherwise, the default connection group is used
You can also seed data from the command line, as part of the Migrations CLI tools, if you don't want to create a dedicated controller:
php spark db:seed TestSeederUsing the command line, you can easily generate seed files:
php spark make:seeder user --suffixThe above command outputs UserSeeder.php file located at app/Database/Seeds directory.
You can supply the root namespace where the seed file will be stored by supplying the --namespace option:
For Unix:
php spark make:seeder MySeeder --namespace Acme\\BlogFor Windows:
php spark make:seeder MySeeder --namespace Acme\BlogIf Acme\Blog is mapped to app/Blog directory, then this command will generate MySeeder.php at app/Blog/Database/Seeds directory.
Supplying the --force option will overwrite existing files in destination.