Skip to content

Commit 93d7c8a

Browse files
authored
Fix up seed docs. (#869)
* Fix up seed docs. * Update docs/en/seeding.rst
1 parent b7d251a commit 93d7c8a

10 files changed

Lines changed: 278 additions & 302 deletions

File tree

docs/en/index.rst

Lines changed: 15 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Here's an example of a migration::
6666
* https://book.cakephp.org/migrations/3/en/writing-migrations.html#the-change-method
6767
* @return void
6868
*/
69-
public function change()
69+
public function change(): void
7070
{
7171
$table = $this->table('products');
7272
$table->addColumn('name', 'string', [
@@ -281,7 +281,7 @@ The command line above will generate a migration file that resembles::
281281
* https://book.cakephp.org/migrations/3/en/writing-migrations.html#the-change-method
282282
* @return void
283283
*/
284-
public function change()
284+
public function change(): void
285285
{
286286
$table = $this->table('products');
287287
$table->addColumn('name', 'string', [
@@ -323,7 +323,7 @@ Executing the command line above will generate::
323323

324324
class AddPriceToProducts extends BaseMigration
325325
{
326-
public function change()
326+
public function change(): void
327327
{
328328
$table = $this->table('products');
329329
$table->addColumn('price', 'decimal', [
@@ -352,7 +352,7 @@ will generate::
352352

353353
class AddNameIndexToProducts extends BaseMigration
354354
{
355-
public function change()
355+
public function change(): void
356356
{
357357
$table = $this->table('products');
358358
$table->addColumn('name', 'string')
@@ -383,7 +383,7 @@ Executing the command line above will generate::
383383

384384
class AddFullDescriptionToProducts extends BaseMigration
385385
{
386-
public function change()
386+
public function change(): void
387387
{
388388
$table = $this->table('products');
389389
$table->addColumn('full_description', 'string', [
@@ -418,7 +418,7 @@ will generate::
418418

419419
class AlterPriceOnProducts extends BaseMigration
420420
{
421-
public function change()
421+
public function change(): void
422422
{
423423
$table = $this->table('products');
424424
$table->changeColumn('name', 'float');
@@ -443,7 +443,7 @@ creates the file::
443443

444444
class RemovePriceFromProducts extends BaseMigration
445445
{
446-
public function up()
446+
public function up(): void
447447
{
448448
$table = $this->table('products');
449449
$table->removeColumn('price')
@@ -694,96 +694,8 @@ value. If you use it, it will mark all found migrations as migrated:
694694
Seed classes are a good way to populate your database with default or starter
695695
data. They are also a great way to generate data for development environments.
696696

697-
By default, seed files will be looked for in the ``config/Seeds`` directory of
698-
your application. See the :doc:`seeding` for how to build seed classes.
699-
700-
As for migrations, a ``bake`` interface is provided for seed files:
701-
702-
.. code-block:: bash
703-
704-
# This will create a ArticlesSeed.php file in the directory config/Seeds of your application
705-
# By default, the table the seed will try to alter is the "tableized" version of the seed filename
706-
bin/cake bake seed Articles
707-
708-
# You specify the name of the table the seed files will alter by using the ``--table`` option
709-
bin/cake bake seed Articles --table my_articles_table
710-
711-
# You can specify a plugin to bake into
712-
bin/cake bake seed Articles --plugin PluginName
713-
714-
# You can specify an alternative connection when generating a seeder.
715-
bin/cake bake seed Articles --connection connection
716-
717-
# Include data from the Articles table in your seed.
718-
bin/cake bake seed --data Articles
719-
720-
By default, it will export all the rows found in your table. You can limit the
721-
number of rows exported by using the ``--limit`` option:
722-
723-
.. code-block:: bash
724-
725-
# Will only export the first 10 rows found
726-
bin/cake bake seed --data --limit 10 Articles
727-
728-
If you only want to include a selection of fields from the table in your seed
729-
file, you can use the ``--fields`` option. It takes the list of fields to
730-
include as a comma separated value string:
731-
732-
.. code-block:: bash
733-
734-
# Will only export the fields `id`, `title` and `excerpt`
735-
bin/cake bake seed --data --fields id,title,excerpt Articles
736-
737-
.. tip::
738-
739-
Of course you can use both the ``--limit`` and ``--fields`` options in the
740-
same command call.
741-
742-
To seed your database, you can use the ``seed`` subcommand:
743-
744-
.. code-block:: bash
745-
746-
# Without parameters, the seed subcommand will run all available seeders
747-
# in the target directory, in alphabetical order.
748-
bin/cake migrations seed
749-
750-
# You can specify only one seeder to be run using the `--seed` option
751-
bin/cake migrations seed --seed ArticlesSeed
752-
753-
# You can run seeders from an alternative directory, relative to config
754-
bin/cake migrations seed --source AlternativeSeeds
755-
756-
# You can run seeders from a plugin
757-
bin/cake migrations seed --plugin PluginName
758-
759-
# You can run seeders from a specific connection
760-
bin/cake migrations seed --connection connection
761-
762-
Be aware that, as opposed to migrations, seeders are not tracked, which means
763-
that the same seeder can be applied multiple times.
764-
765-
Calling a Seeder from another Seeder
766-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
767-
768-
Usually when seeding, the order in which to insert the data must be respected
769-
to not encounter constraints violations. Since Seeders are executed in the
770-
alphabetical order by default, you can use the ``\Migrations\BaseSeed::call()``
771-
method to define your own sequence of seeders execution::
772-
773-
use Migrations\BaseSeed;
774-
775-
class DatabaseSeed extends BaseSeed
776-
{
777-
public function run(): void
778-
{
779-
$this->call('AnotherSeed');
780-
$this->call('YetAnotherSeed');
781-
782-
// You can use the plugin dot syntax to call seeders from a plugin
783-
$this->call('PluginName.FromPluginSeed');
784-
}
785-
}
786-
697+
By default, seeds will be looked for in the ``config/Seeds/`` directory of
698+
your application. See the :doc:`seeding` for how to build and use seed classes.
787699

788700
``dump`` : Generating a dump file for the diff baking feature
789701
-------------------------------------------------------------
@@ -984,7 +896,7 @@ adding new tables to the database, you can use the second argument of the
984896

985897
class CreateProductsTable extends BaseMigration
986898
{
987-
public function change()
899+
public function change(): void
988900
{
989901
$table = $this->table('products', ['id' => false, 'primary_key' => ['id']]);
990902
$table
@@ -1023,7 +935,7 @@ it to the table declaration::
1023935

1024936
public bool $autoId = false;
1025937

1026-
public function up()
938+
public function up(): void
1027939
{
1028940
$table = $this->table('products');
1029941
$table
@@ -1060,7 +972,7 @@ default one, you can define it with the ``table()`` method, as an option::
1060972

1061973
class CreateCategoriesTable extends BaseMigration
1062974
{
1063-
public function change()
975+
public function change(): void
1064976
{
1065977
$table = $this
1066978
->table('categories', [
@@ -1111,14 +1023,14 @@ Renaming a table
11111023
The plugin gives you the ability to rename a table, using the ``rename()``
11121024
method. In your migration file, you can do the following::
11131025

1114-
public function up()
1026+
public function up(): void
11151027
{
11161028
$this->table('old_table_name')
11171029
->rename('new_table_name')
11181030
->update();
11191031
}
11201032

1121-
public function down()
1033+
public function down(): void
11221034
{
11231035
$this->table('new_table_name')
11241036
->rename('old_table_name')
@@ -1164,7 +1076,7 @@ to alert developers about new migrations that have not been applied::
11641076
->add(new PendingMigrationsMiddleware($config))
11651077
... // rest
11661078

1167-
You can add `'app'` config key set to `false` if you are only interested in
1079+
You can add ``'app'`` config key set to ``false`` if you are only interested in
11681080
checking plugin migrations.
11691081

11701082
You can temporarily disable the migration check by adding

0 commit comments

Comments
 (0)