开发者

How to define "global" find conditions for model in CakePHP?

开发者 https://www.devze.com 2023-03-22 02:15 出处:网络
Is it possible to define find conditions that are always effective in all controllers a开发者_运维问答nd functions that use specific model?

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号