I posted about this last year, but since it has landed in the official plugin, I figured I may as well cover it again :)

For developers coming from other RAD frameworks such as Ruby on Rails, you may be familiar with the ability to create table migrations. In the past, there were multiple migration plugins for CakePHP, and as of the 3.0 release, we finally have an official cakephp/migrations plugin. Huge shoutout to Yves Piquel (@HavokInspiration) for pushing the plugin forward.

Here is a simple migration class:

use Phinx\Migration\AbstractMigration;
class CreatePosts extends AbstractMigration
    public function change()
        $table = $this->table('posts');
        $table->addColumn('name', 'string', [
            'null' => false,
            'default' => null,
            'limit' => 255,
        $table->addColumn('created', 'datetime', [
            'null' => false,
            'default' => null,
        $table->addColumn('modified', 'datetime', [
            'null' => false,
            'default' => null,
        $table->addIndex(['name'], [
            'unique' => false,
            'name' => 'BY_NAME',

Raise your hand if you would have wanted to create that from scratch.


I thought as much.

With the new plugin, we now have the ability to generate those from the command-line. Here is a short example:

bin/cake bake migration create_posts name:string created modified

A few things:

  • id fields are autocreated. This is a feature of the Phinx project we lean on for developing PHP migrations. One-clap to Rob Morgan for managing this project on behalf of the PHP community at large.
  • Certain fields have automatic types set if left null. You can always override them though.
  • The classname is the UpperCamelCase inflection of the first argument, and means something different depending upon the name:
    • create_table /^(Create)(.*)/: Creates the specified table
    • drop_table /^(Drop)(.*)/: Drops the specified table. Ignores specified field arguments.
    • add_field /^(Add).*(?:To)(.*)/: Adds fields to the specified table
    • remove_field /^(Remove).*(?:From)(.*)/: Removes fields from the specified table
    • alter_table /^(Alter)(.*)/: Alters the specified table. The alter_table command can be used as an alias for CreateTable and AddField.

This plugin is an absolute must-use for anything prototyping an application which schema changes. No one wants to write the SQL for them, and that goes double for writing actual schema migration files. I recommend reading the docs on this excellent feature to learn more.


I normally deploy my code on platforms such as Heroku or Dokku where composer commands are automatically run for me. Here is my scripts field in my composer.json:

"scripts": {
    "compile": [
        "bin/cake migrations migrate",
        "bin/cake migrations migrate --plugin Blog",
        "bin/cake orm_cache build"
    "post-install-cmd": "App\\Console\\Installer::postInstall",
    "post-autoload-dump": "Cake\\Composer\\Installer\\PluginInstaller::postAutoloadDump"

Other than the normal stuff in there for application installation, you’ll notice I have a scripts.compile key which maps to a list of migration commands to run. I run the migrations for my core application as well as the blog plugin I use in this particular application. Of course, don’t forget to rebuild your orm cache, otherwise your app might not be aware of schema changes! Migrations are now fully automatic for this application, and I don’t need to worry about going in and manually altering anything!