本文介绍了如何定义“全局”在CakePHP中找到模型的条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以定义在所有使用特定模型的控制器和函数中始终有效的查找条件?
Is it possible to define find conditions that are always effective in all controllers and 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.
控制器:
$productsInStock = $this->Product->getProductsInStock();
型号:
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
这篇关于如何定义“全局”在CakePHP中找到模型的条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!