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.


Your comments

GabeK
Tue, Aug 16th 2011, 18:20

Thanks! This was exactly what I was looking for. I was about to create a class var to act as a flag in beforeFind - but this is definitely better!