Skip to content

Commit 6f70ca4

Browse files
authored
Merge pull request #905 from cakephp/issue-904
Add test and expand bake template docs
2 parents 0995f73 + 483b441 commit 6f70ca4

5 files changed

Lines changed: 136 additions & 3 deletions

File tree

docs/en/seeding.rst

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ Database Seeding
22
################
33

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

87
.. note::
98

10-
Database seeding is entirely optional, and Migrations does not create a `Seeds`
9+
Database seeding is entirely optional, and Migrations does not create a Seeds
1110
directory by default.
1211

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

82+
.. _custom-seed-migration-templates:
83+
84+
Customizing Seed and Migration templates
85+
----------------------------------------
86+
87+
Because migrations uses `bake <https://book.cakephp.org/bake>`__ under the hood
88+
you can customize the templates that migrations uses for creating seeds and
89+
migrations by creating templates in your application. Custom templates for
90+
migrations should be on one of the following paths:
91+
92+
- ``ROOT/templates/plugin/Migrations/bake/``
93+
- ``ROOT/templates/bake/``
94+
95+
For example the seed template is ``Seed/seed.twig`` and its full path would be
96+
**ROOT/templates/plugin/Migrations/bake/Seed/seed.twig**
97+
8398
The BaseSeed Class
8499
==================
85100

docs/en/writing-migrations.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,6 +1631,11 @@ plan migrations when more than one table is involved.
16311631
}
16321632
}
16331633
1634+
Changing templates
1635+
------------------
1636+
1637+
See :ref:`custom-seed-migration-templates` for how to customize the templates
1638+
used to generate migrations.
16341639

16351640

16361641
Using the Query Builder

tests/TestCase/Command/BakeSeedCommandTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,27 @@ public function testBasicBaking()
8383
$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
8484
}
8585

86+
/**
87+
* Test using an application template
88+
*
89+
* @return void
90+
*/
91+
public function testBakeWithApplicationTemplate()
92+
{
93+
copy(
94+
ROOT . '/App/Template/plugin/Migrations/bake/Seed/custom-seed.twig',
95+
ROOT . '/App/Template/plugin/Migrations/bake/Seed/seed.twig',
96+
);
97+
$this->generatedFiles[] = ROOT . '/config/Seeds/ArticlesSeed.php';
98+
$this->generatedFiles[] = ROOT . '/App/Template/plugin/Migrations/bake/Seed/seed.twig';
99+
100+
$this->exec('bake seed Articles --connection test');
101+
102+
$this->assertExitCode(BaseCommand::CODE_SUCCESS);
103+
$result = file_get_contents($this->generatedFiles[0]);
104+
$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
105+
}
106+
86107
/**
87108
* Test with data, all fields, no limit
88109
*
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use Migrations\BaseSeed;
5+
6+
/**
7+
* Articles seed.
8+
*
9+
* CUSTOM APP TEMPLATE
10+
*/
11+
class ArticlesSeed extends BaseSeed
12+
{
13+
/**
14+
* Run Method.
15+
*
16+
* Write your database seeder using this method.
17+
*
18+
* More information on writing seeds is available here:
19+
* https://book.cakephp.org/migrations/4/en/seeding.html
20+
*
21+
* @return void
22+
*/
23+
public function run(): void
24+
{
25+
$data = [];
26+
27+
$table = $this->table('articles');
28+
$table->insert($data)->save();
29+
}
30+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
{#
2+
/**
3+
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4+
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
5+
*
6+
* Licensed under The MIT License
7+
* For full copyright and license information, please see the LICENSE.txt
8+
* Redistributions of files must retain the above copyright notice.
9+
*
10+
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
11+
* @link https://cakephp.org CakePHP(tm) Project
12+
* @since 0.1.0
13+
* @license https://www.opensource.org/licenses/mit-license.php MIT License
14+
*/
15+
#}
16+
<?php
17+
declare(strict_types=1);
18+
19+
{% if backend == "builtin" %}
20+
use Migrations\BaseSeed;
21+
22+
/**
23+
* {{ name }} seed.
24+
*
25+
* CUSTOM APP TEMPLATE
26+
*/
27+
class {{ name }}Seed extends BaseSeed
28+
{% else %}
29+
use Migrations\AbstractSeed;
30+
31+
/**
32+
* {{ name }} seed.
33+
*/
34+
class {{ name }}Seed extends AbstractSeed
35+
{% endif %}
36+
{
37+
/**
38+
* Run Method.
39+
*
40+
* Write your database seeder using this method.
41+
*
42+
* More information on writing seeds is available here:
43+
{% if backend == "builtin" %}
44+
* https://book.cakephp.org/migrations/4/en/seeding.html
45+
{% else %}
46+
* https://book.cakephp.org/phinx/0/en/seeding.html
47+
{% endif %}
48+
*
49+
* @return void
50+
*/
51+
public function run(): void
52+
{
53+
{% if records %}
54+
$data = {{ records | raw }};
55+
{% else %}
56+
$data = [];
57+
{% endif %}
58+
59+
$table = $this->table('{{ table }}');
60+
$table->insert($data)->save();
61+
}
62+
}

0 commit comments

Comments
 (0)