diff --git a/docs/en/seeding.rst b/docs/en/seeding.rst index e42a9e7a..01bbf5b7 100644 --- a/docs/en/seeding.rst +++ b/docs/en/seeding.rst @@ -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 @@ -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 `__ 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 ================== diff --git a/docs/en/writing-migrations.rst b/docs/en/writing-migrations.rst index e0e71a15..266e3ec4 100644 --- a/docs/en/writing-migrations.rst +++ b/docs/en/writing-migrations.rst @@ -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 diff --git a/tests/TestCase/Command/BakeSeedCommandTest.php b/tests/TestCase/Command/BakeSeedCommandTest.php index 0ab7be7b..dd286bf9 100644 --- a/tests/TestCase/Command/BakeSeedCommandTest.php +++ b/tests/TestCase/Command/BakeSeedCommandTest.php @@ -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 * diff --git a/tests/comparisons/Seeds/testBakeWithApplicationTemplate.php b/tests/comparisons/Seeds/testBakeWithApplicationTemplate.php new file mode 100644 index 00000000..32f3a736 --- /dev/null +++ b/tests/comparisons/Seeds/testBakeWithApplicationTemplate.php @@ -0,0 +1,30 @@ +table('articles'); + $table->insert($data)->save(); + } +} diff --git a/tests/test_app/App/Template/plugin/Migrations/bake/Seed/custom-seed.twig b/tests/test_app/App/Template/plugin/Migrations/bake/Seed/custom-seed.twig new file mode 100644 index 00000000..c732afca --- /dev/null +++ b/tests/test_app/App/Template/plugin/Migrations/bake/Seed/custom-seed.twig @@ -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 + */ +#} +table('{{ table }}'); + $table->insert($data)->save(); + } +}