php cakephp OR 条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8485950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cakephp OR condition
提问by eldamar
Originaly postedon cakephp Q&A but i'll put it up here in hope of getting some answers.
最初发布在 cakephp Q&A 上,但我会把它放在这里希望得到一些答案。
I have a bunch of companies that has a status of 0 as default but sometimes get a higher status. Now i want to use the high status if exists but revert to 0 if not. i have tried a bunch of different approaches but i always get either only the ones with status 0 or the ones with the status i want, never giving me status if exists and 0 if not.
我有很多公司的默认状态为 0,但有时会获得更高的状态。现在我想使用高状态(如果存在)但如果不存在则恢复为 0。我尝试了许多不同的方法,但我总是只得到状态为 0 的方法或具有我想要的状态的方法,如果存在则从不给我状态,如果不存在则为 0。
Gives me only the status i specify, not giving me the ones with status 0:
只给我指定的状态,而不给我状态为 0 的状态:
'Company' => array (
'conditions' => array (
'OR' => array(
'Company.status' => 0,
'Company.status' => $status,
)
)
)
Gives me only status of 0:
只给我 0 的状态:
'Company' => array (
'conditions' => array (
'OR' => array(
'Company.status' => $status,
'Company.status' => 0
)
)
)
Status definition and retrieving data in code:
状态定义和代码检索数据:
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.
}
I changed the name for the models for this question just to make more sense, people are generaly frighten away when i tell them i work with cakephp for my flex application. I guess the logic to this question doesn't make sense but trust me that it makes sense in my application.
我为这个问题更改了模型的名称只是为了更有意义,当我告诉他们我为我的 flex 应用程序使用 cakephp 时,人们通常会感到害怕。我想这个问题的逻辑没有意义,但相信我,它在我的应用程序中是有意义的。
Thanks!
谢谢!
回答by Vigrond
Try
尝试
'Company' => array (
'conditions' => array (
'OR' => array(
array('Company.status' => 0),
array('Company.status' => $status),
)
)
)
In the cookbook it says to wrap the or conditions in arrays if they are pertaining to the same field http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#complex-find-conditions
在食谱中,如果它们属于同一字段http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#complex-find-conditions,则将它们包装在数组中
回答by nIcO
I'm not sure to have understood what results you expect. If you want to retrieve all records having status = 0, plus let's say the one having status = 3, you could use an 'IN' instead of an 'OR'.
我不确定是否理解您期望的结果。如果要检索状态 = 0 的所有记录,再加上状态 = 3 的记录,则可以使用“IN”而不是“OR”。
In Cake, you would write it like this:
在 Cake 中,你可以这样写:
$status = 3;
$conditions = array('Company.status' => array(0, $status));
回答by Rashid Khan
You can also fetch record by using following method: put values in an array e.g. $arr=array(1,2);
您还可以使用以下方法获取记录:将值放入数组中,例如 $arr=array(1,2);
$res = $this->Model->find('all', array(
'conditions' =>array('Model.filedname'=>$arr),
'model.id' => 'desc'
));
I hope you will find answer.
我希望你能找到答案。