[Solved] updateAll cakePHP unable to fire callback

Tuesday, April 17, 2012 0 Comments

Still on logging sql-queries to database. This day, I was stuck on updateAll that was unable to fire callback afterSave(), not like normal update using save(). I thought my scripts was wrong -and yeah it was, even just a lil. And, damn, it still doesn't work! The other, delete and insert are normal. I mean, it's executed and written in log table like expected. In my case, the updateAll() is just executed and nothing happen in log table.

I still had positive-thinking about 'there was something wrong within my script' that I still don't know what is it. Had a thought what if updateAll() is not firing the afterSave callback, so I did a lil bit experiment. Just say it try-and-error. 

So, finally my thought about 'afterSave() is not fired by updateAll' was strengthen. I was so sure if indeed it was. And, I did googling and tadaaa.. By default, yes, updateAll() is not designed to fire callback because of security reason. 
This is by design. Since updateAll can take any SQL expression for updating conditions its not possible to know for which individual records to run the callbacks without adding lot of overhead like first doing finds and making list of ids of records that will be updated.

Clear enough. But like @mark_story told me :
Of course, its just PHP,,,

That means,, everything is possible :D What did I do? Let's go to the how-to section..

How To Make updateAll() Fire Callback

Note, Am using cakePHP 1.3.
Again, from another source I got from did a google-search, I found this old article : http://nuts-and-bolts-of-cakephp.com/2010/01/27/make-updateall-fire-behavior-callbacks/ that quiet help me. At, least, I could imagine how. And from the comment, I've taken to croogo-project on github. And it works!
  1. The concept is, override existing updateAll(). To do so, make sure you have app_model.php file.
  2. Show time : here is the code http://pastebin.com/w58bVYWB
At least, it's work for me, on my app.. :)

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Do you think I forgot to change this text? No, dude. I did it on purpose ahahahay..