Zie*_*emo 5 cakephp cakephp-2.0 cakephp-2.1 cakephp-model
是否可以为模型定义全局条件?
我有2个型号:User和Student.在数据库中,它们都使用表,users但每个学生已设置parent_id为其所有者(在同一个表中设置),而每个用户都parent_id设置为Null.
当我使用例如
$this->find('all');
Run Code Online (Sandbox Code Playgroud)
在Student模型中我想强制Cake只返回数据库表users中的那些记录parent_id != Null.
所以问题是 - 我可以在模型中以某种方式定义全局条件吗?像这样的东西:
public $conditions = array('Student.parent_id !=' => Null);
您可以在find之前使用来修改为模型发出的所有查询:
function beforeFind(array $queryData) {
$queryData['conditions'][]['NOT'][$this->alias . '.parent_id'] = null;
return $queryData;
}
Run Code Online (Sandbox Code Playgroud)
请小心使用此技术不要覆盖现有条件(请注意额外的[]),否则对"not parent_id 2"的查询将变为"not parent_id null".
| 归档时间: |
|
| 查看次数: |
560 次 |
| 最近记录: |