Documentation
Database: Seeding
Introduction
Cygnite provides a simple method to seed your database with test data. All seed class are stored in src/Apps/Resources/Database/Seeding/ folder. You can keep any name for seeder class, but it is good to follow meaningful name such as UserTable, RoleTable etc. The DatabaseTable class is to define all seeder classes, only those classes will use to seed test data in your database tables.
Creating Seeder Class - Structure
You may create a seeder class as below. It will should extend migration class and must have execute method.
namespace Apps\Resources\Database\Seeding;
use Cygnite\Database\Migration;
use Cygnite\Database\Table\Schema;
use Cygnite\Database\Table\Seeder;
/**
* Class ProductTable
*
* @package Apps\Resources\Database\Seeding
*/
class ProductTable extends Migration
{
public function execute()
{
$data = [
'name' => 'Cyrus',
'description' => 'Cyrus is simple, lightweight but powerful ORM.',
'meta' => 'orm',
'created_by' => 'cyrus',
];
$this->insert('product', $data);
}
}
Registering Seeder Class
Once you created seeder class, you need to register it in DatabaseTable seeder class. Add a newly created seeder class name in seeders array stack. So that system can run it.
namespace Apps\Resources\Database\Seeding;
use Cygnite\Database\Table\Seeder;
/**
* Class DatabaseTable
*
* @package Apps\Resources\Database\Seeding
*/
class DatabaseTable extends Seeder
{
protected $seeders = [
'ProductTable',
];
public function run()
{
$this->execute();
}
}
Calling Multiple Seeder
Inside DatabaseTable add multiple seeder class in a stack. So that system will understand all are valid seeder and runnable.
protected $seeders = [
'ProductTable',
'UserTable',
'RoleTable',
];
Running Seeders
Once you have written and registered your seeder classes, you may use the database:seed command to seed your database. By default database:seed command runs all seeder class to seed database.However, you may specify seeder class name in command to run specific seeder class individually.
php craft database:seed
php craft database:seed UserTable,RoleTable