Phinx: Confusion about using save() vs create() / update() in change()

Created on 1 Jun 2016  路  4Comments  路  Source: cakephp/phinx

Hi.

The documentation says that using save() in the change() method is not the right thing to do. Instead we need to use create() or update().

But the save() method does seem to work.

     public function change()
     {
         $this->table('contacts')
             ->addColumn('photo', 'string', ['null' => true, 'after' => 'allowMarketing', 'limit' => 100])
             ->save();
     }

The save() method seems to make the right decision to create() or update() depending upon the existence of the table.

Most helpful comment

The save() method makes the decision whether it should create or update the table and it works like you discovered. But in my opinion, you should use create() or update() explicitly because you'll get an exception when the database state isn't what you expect.

For example, when there is a table with the name you are trying to create. With create() you'll get an error but with save() it adds columns to that table. And when you want to add columns to table but the table doesn't exist for some reason then migration create() will throw an exception but addColumn(...)->save() will create that maybe partial table.

But as said save() works too.

All 4 comments

The save() method makes the decision whether it should create or update the table and it works like you discovered. But in my opinion, you should use create() or update() explicitly because you'll get an exception when the database state isn't what you expect.

For example, when there is a table with the name you are trying to create. With create() you'll get an error but with save() it adds columns to that table. And when you want to add columns to table but the table doesn't exist for some reason then migration create() will throw an exception but addColumn(...)->save() will create that maybe partial table.

But as said save() works too.

When updating tables, when one use update and when save?
It's not clear from the docs.

save() is mostly legacy. It can currently infer if you meant update or create but it is better to be specific about it.

This was written a LONG time ago and way before I got to grips with phinx. Personally, if I could, I'd remove the entire issue as it is just noise and makes the invalid assumptions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

J-Fricke picture J-Fricke  路  3Comments

kabirbaidhya picture kabirbaidhya  路  4Comments

Jacco-V picture Jacco-V  路  4Comments

omer-sds picture omer-sds  路  4Comments

elct9620 picture elct9620  路  4Comments