Data Seed plugin for CakePHP 3
This plugin extends CakePHP migrations seeding functionality by allowing to bake seed files with data included.
Requirements
- CakePHP 3.0+
Installation
You can install this plugin into your CakePHP application using composer.
Run the following command
composer require andrej-griniuk/cakephp-data-seed
Configuration
You can load the plugin using the shell command:
bin/cake plugin load DataSeed
Or you can manually add the loading statement in the config/boostrap.php file of your application:
Plugin::load('DataSeed');
Additionally, you will need to configure the default
database configuration in your config/app.php file.
Usage
This plugin does pretty much same thing as bin/cake bake seed
with that difference that data from the given table will be added to the generated seed file. E.g. running the following command:
bin/cake bake data_seed articles
will generate config/Seeds/ArticlesSeed.php
file:
use Migrations\AbstractSeed;
/**
* Articles seed.
*/
class ArticlesSeed 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
*
* @return void
*/
public function run()
{
$data = [
[
'id' => '1',
'author_id' => '1',
'title' => 'First Article',
'body' => 'First Article Body',
'published' => 'Y',
],
[
'id' => '2',
'author_id' => '3',
'title' => 'Second Article',
'body' => 'Second Article Body',
'published' => 'Y',
],
[
'id' => '3',
'author_id' => '1',
'title' => 'Third Article',
'body' => 'Third Article Body',
'published' => 'Y',
],
];
$table = $this->table('articles');
$table->insert($data)->save();
}
}
All the options available to bin/cake bake seed
are available here as well. Plus you can specify table fields you want to be included (by default all fields are) using --fields
option
bin/cake bake data_seed --fields=author_id,title,body articles
You can also specify max number of rows to fetch using --limit
option:
bin/cake bake data_seed --limit=5 articles
Warning: it has been tested with MySQL. As far as I know it will cause issue in PostgreSQL when inserting auto increment field values.
Bugs & Feedback
https://github.com/andrej-griniuk/cakephp-data-seed/issues
License
Copyright (c) 2016, Andrej Griniuk and licensed under The MIT License.