How to define "global" find conditions for model in CakePHP?
Is it possible to define find conditions that are always effective in all controllers a开发者_运维问答nd functions that use specific model?
For example if I want to return only products that are in stock no matter what. Maybe somewhere in model:
conditions => array('inStock >' => 0)
I think you could try to do a function on the model, and then call it in controller with a simple line.
Controller:
$productsInStock = $this->Product->getProductsInStock();
Model:
function getProductsInStock() {
$produtcsInStock = $this->find('all', array('conditions' => array('inStock >' => 0)));
return $productsInStock;
}
Or try this Link, I think it will help. I just don't know nothing about callbacks: http://book.cakephp.org/view/1049/beforeFind
you can add that logic by overriding beforeFind() in the specific model: http://book.cakephp.org/view/1049/beforeFind
remember to call parent::beforeFind(); at the beginning.
精彩评论