How to turn off CakePHP Model Find Callbacks
Something I had a major headache with today was the very useful callback methods in CakePHP's Model class. I was using the afterFind() method to check a record and then perform a lookup on the same table, which naturally ended up creating an infinite loop. The solution obviously was to turn off callbacks on the lookup in the afterFind().
After looking at the Cookbook, I discovered the solution (and didn't know existed until now) was you can pass 'callbacks' => false to the Model::find() method to turn off callback rendering completely. You can also either specify 'before' or 'after' to only render either one of them callbacks.
Here's an example of what I mean:
$this->Model->find('all', array( 'conditions' => array(...), 'order' => array(...), 'callbacks' => false ));
So just like parsing your conditions you just parse the callbacks parameter - simples!
Posted: Mon, Jun 13th 2011, 19:36
Leave a comment
To leave a comment please fill out the form below.
- June 2011 (2)