Search code examples
phpcakephpcakephp-3.0database-migrationphinx

CakePHP 3.x Database Migration Plugin: Is there a way to Change a table field?


I am trying to figure out how best to modify a MySQL Table's existing Column using the CakePHP Migrations plugin. I do not need to add or drop the column, I simply want to modify a string column's length.

Currently the column is defined as a varchar(50); I am repurposing the column and wish to define it as varchar(2000).

The goal of the migration is to be part of an automated deployment taking place on a standard CakePHP web app installation on a typical web server.

Near as I can tell, it looks like the only way (other than an ALTER statement) to accomplish this using the Migrations Plugin would be to:

  1. rename the column
  2. add the new column
  3. Move/copy the existing data to the new column
  4. drop the old column

Perhaps I have missed the discussion in the documents and countless tutorials and how to's out there on a better way to accomplish this, but this seems like a cumbersome and self defeating method.

I have been through both the CakePHP Migration Plugin's documentation and the Phinx's documentation but am failing to see the recommended method for this change. I appreciate any input for this.


Solution

  • Unfortunately the Phinx docs aren't that complete, there seem to be various undocumented methods, like the one you are looking for: \Phinx\Db\Table::changeColumn()

    The following should work

    $table = $this->table('table_name');
    $table->changeColumn('column_name', 'string', [
        'limit' => 2000
    ]);
    $table->update();