Skip to content

Commit 24e53a2

Browse files
authored
docs: Add anonymous migration classes documentation (#1056)
Document the --style anonymous option for bake migration including: - Benefits of anonymous migration classes - Command usage example - Generated code structure - Global Migrations.style configuration option
1 parent 6178d64 commit 24e53a2

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

docs/en/writing-migrations.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,60 @@ class MyNewMigration extends BaseMigration
4040
}
4141
```
4242

43+
### Anonymous Migration Classes
44+
45+
Migrations also supports generating anonymous migration classes, which use PHP's
46+
anonymous class feature instead of named classes. This style is useful for:
47+
48+
- Avoiding namespace declarations
49+
- Better PHPCS compatibility (no class name to filename matching required)
50+
- Simpler file structure without named class constraints
51+
- More readable filenames like `2024_12_08_120000_CreateProducts.php`
52+
53+
To generate an anonymous migration class, use the `--style anonymous` option:
54+
55+
``` bash
56+
$ bin/cake bake migration CreateProducts --style anonymous
57+
```
58+
59+
This generates a migration file using an anonymous class:
60+
61+
``` php
62+
<?php
63+
declare(strict_types=1);
64+
65+
use Migrations\BaseMigration;
66+
67+
return new class extends BaseMigration
68+
{
69+
/**
70+
* Change Method.
71+
*
72+
* More information on this method is available here:
73+
* https://book.cakephp.org/migrations/5/en/migrations.html#the-change-method
74+
* @return void
75+
*/
76+
public function change(): void
77+
{
78+
}
79+
};
80+
```
81+
82+
Both traditional and anonymous migration classes work identically at runtime and can
83+
be used interchangeably within the same project.
84+
85+
You can set the default migration style globally in your application configuration:
86+
87+
``` php
88+
// In config/app.php or config/app_local.php
89+
'Migrations' => [
90+
'style' => 'anonymous', // or 'traditional'
91+
],
92+
```
93+
94+
This configuration also applies to seeds, allowing you to use consistent styling
95+
across your entire project.
96+
4397
## The Change Method
4498

4599
Migrations supports 'reversible migrations'. In many scenarios, you

0 commit comments

Comments
 (0)