Defining global conditions in Model

别说谁变了你拦得住时间么 提交于 2019-11-29 11:15:51

Use beforeFind

You can use before find to modify all queries issued for a model:

function beforeFind(array $queryData) {
    $queryData['conditions'][]['NOT'][$this->alias . '.parent_id'] = null;
    return $queryData;
}

Be careful using this technique to not overwrite existing conditions (note the extra []) otherwise a query for "not parent_id 2" becomes "not parent_id null".

you could use the afterFind callback to alter your finds in the model

public function afterFind($results, $primary = false) {
    foreach ($results as $key => $val) {
        if ($val['parent_id'] == NULL) { //no parent_id set then remove that part of the results
           unset($results[$key]);
        }
    }
    return $results;
}

reference: http://book.cakephp.org/2.0/en/models/callback-methods.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!