Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions docs/en/seeding.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ Database Seeding
################

Seed classes are a great way to easily fill your database with data after
it's created. By default, they are stored in the `seeds` directory; however, this
path can be changed in your configuration file.
it's created. By default, they are stored in the ``config/Seeds`` directory.

.. note::

Database seeding is entirely optional, and Migrations does not create a `Seeds`
Database seeding is entirely optional, and Migrations does not create a Seeds
directory by default.

Creating a New Seed Class
Expand Down Expand Up @@ -80,6 +79,22 @@ include as a comma separated value string:
Of course you can use both the ``--limit`` and ``--fields`` options in the
same command call.

.. _custom-seed-migration-templates:

Customizing Seed and Migration templates
----------------------------------------

Because migrations uses `bake <https://book.cakephp.org/bake>`__ under the hood
you can customize the templates that migrations uses for creating seeds and
migrations by creating templates in your application. Custom templates for
migrations should be on one of the following paths:

- ``ROOT/templates/plugin/Migrations/bake/``
- ``ROOT/templates/bake/``

For example the seed template is ``Seed/seed.twig`` and its full path would be
**ROOT/templates/plugin/Migrations/bake/Seed/seed.twig**

The BaseSeed Class
==================

Expand Down
5 changes: 5 additions & 0 deletions docs/en/writing-migrations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1631,6 +1631,11 @@ plan migrations when more than one table is involved.
}
}

Changing templates
------------------

See :ref:`custom-seed-migration-templates` for how to customize the templates
used to generate migrations.


Using the Query Builder
Expand Down
21 changes: 21 additions & 0 deletions tests/TestCase/Command/BakeSeedCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,27 @@ public function testBasicBaking()
$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
}

/**
* Test using an application template
*
* @return void
*/
public function testBakeWithApplicationTemplate()
{
copy(
ROOT . '/App/Template/plugin/Migrations/bake/Seed/custom-seed.twig',
ROOT . '/App/Template/plugin/Migrations/bake/Seed/seed.twig',
);
$this->generatedFiles[] = ROOT . '/config/Seeds/ArticlesSeed.php';
$this->generatedFiles[] = ROOT . '/App/Template/plugin/Migrations/bake/Seed/seed.twig';

$this->exec('bake seed Articles --connection test');

$this->assertExitCode(BaseCommand::CODE_SUCCESS);
$result = file_get_contents($this->generatedFiles[0]);
$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
}

/**
* Test with data, all fields, no limit
*
Expand Down
30 changes: 30 additions & 0 deletions tests/comparisons/Seeds/testBakeWithApplicationTemplate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);

use Migrations\BaseSeed;

/**
* Articles seed.
*
* CUSTOM APP TEMPLATE
*/
class ArticlesSeed extends BaseSeed
{
/**
* Run Method.
*
* Write your database seeder using this method.
*
* More information on writing seeds is available here:
* https://book.cakephp.org/migrations/4/en/seeding.html
*
* @return void
*/
public function run(): void
{
$data = [];

$table = $this->table('articles');
$table->insert($data)->save();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{#
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 0.1.0
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/
#}
<?php
declare(strict_types=1);

{% if backend == "builtin" %}
use Migrations\BaseSeed;

/**
* {{ name }} seed.
*
* CUSTOM APP TEMPLATE
*/
class {{ name }}Seed extends BaseSeed
{% else %}
use Migrations\AbstractSeed;

/**
* {{ name }} seed.
*/
class {{ name }}Seed extends AbstractSeed
{% endif %}
{
/**
* Run Method.
*
* Write your database seeder using this method.
*
* More information on writing seeds is available here:
{% if backend == "builtin" %}
* https://book.cakephp.org/migrations/4/en/seeding.html
{% else %}
* https://book.cakephp.org/phinx/0/en/seeding.html
{% endif %}
*
* @return void
*/
public function run(): void
{
{% if records %}
$data = {{ records | raw }};
{% else %}
$data = [];
{% endif %}

$table = $this->table('{{ table }}');
$table->insert($data)->save();
}
}
Loading