[英]cakephp OR condition
最初发布在 cakephp 问答上,但我会把它放在这里希望得到一些答案。
我有一堆公司的默认状态为 0,但有时会获得更高的状态。 现在我想使用高状态(如果存在),但如果不存在则恢复为 0。 我尝试了很多不同的方法,但我总是只得到状态为 0 的方法或具有我想要的状态的方法,如果存在则从不给我状态,如果不给我状态,则为 0。
只给我我指定的状态,而不给我状态为 0 的状态:
'Company' => array (
'conditions' => array (
'OR' => array(
'Company.status' => 0,
'Company.status' => $status,
)
)
)
只给我0的状态:
'Company' => array (
'conditions' => array (
'OR' => array(
'Company.status' => $status,
'Company.status' => 0
)
)
)
代码中的状态定义和检索数据:
function getCountry($id = null, $status = null) {
// Bunch of code for retrieving country with $id and all it's companies etc, all with status 0.
$status_less_companies = $this->Country->find...
if ($status) {
$status_companies = $this->Country->find('first', array(
'conditions' => array(
'Country.id' => $id
),
'contain' => array(
'Product' => array (
'Company' => array (
'conditions' => array (
'OR' => array(
'Company.status' => $status,
'Company.status' => 0
)
)
)
)
)
)
}
// Mergin $status_less_companies and $status_companies and returning data to flex application.
}
我为这个问题更改了模型的名称只是为了更有意义,当我告诉他们我为我的 flex 应用程序使用 cakephp 时,人们通常会害怕。 我想这个问题的逻辑没有意义,但相信我,它在我的应用程序中是有意义的。
谢谢!
尝试
'Company' => array (
'conditions' => array (
'OR' => array(
array('Company.status' => 0),
array('Company.status' => $status),
)
)
)
在食谱中,如果条件或条件属于同一字段,则表示将条件或条件包装在数组中http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#complex-find-conditions
我不确定您希望得到什么结果。 如果要检索状态为0的所有记录,再加上状态为3的记录,则可以使用“ IN”代替“ OR”。
在Cake中,您可以这样编写:
$status = 3;
$conditions = array('Company.status' => array(0, $status));
您也可以使用以下方法获取记录:将值放入数组中,例如$ arr = array(1,2);
$res = $this->Model->find('all', array(
'conditions' =>array('Model.filedname'=>$arr),
'model.id' => 'desc'
));
希望您能找到答案。
$this->loadModel('Color');
$colors = $this->Color->find('all', [
'conditions' => [
'Color.is_blocked' => 0,
'Color.is_deleted' => 0,
'OR' => [
[
'Color.isAdmin' => $user_data['id'],
],
[
'Color.isAdmin' => 0,
]
],
]
]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.