PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, February 22, 2022

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

 February 22, 2022     cakephp, cakephp-3.0, database-migration, phinx, php     No comments   

Issue

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()

  • https://github.com/robmorgan/phinx/blob/v0.4.6/src/Phinx/Db/Table.php#L392
  • https://github.com/robmorgan/phinx/issues/682

The following should work

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


Answered By - ndm
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing