Model::save() now returns an array!

Posted by Felix Geisendörfer, on Nov 03, 2007 - in PHP & CakePHP » News

Just got bitten by this one when updating to the latest version of CakePHP. If you use code like this in your app:

php
  1. if ($this->User->save() === true) {
  2.    // Do stuff
  3. }

Then you're in for a surprise. Because as of revision 5895 Model::save() now returns Model::data on success if its not empty. Now most of us do not use strict comparison for checking the return value of Model::save(), but I was stupid enough to do it as part of my new "fail hard fast" strategy : ). So suddenly I had stuff blowing up all over the place.

Oh well, thats the price you pay for being one of the cool kids and staying with SVN:HEAD. However, its definitely worth it because there are new goodies added to the core every day. And in fact, Model::save() returning an array is one of them. Because previously you had to make your own copy of Model::data if you needed it after a save() operation because Model::save() always clears Model::data for you.

So its all good, just be sure to check your app for strict Model::save return value comparisons to avoid surprises.

-- Felix Geisendörfer aka the_undefined