cakephp OR condition(cakephp OR 条件)
问题描述
最初张贴在cakephp Q&A上,但我把它放在这里希望得到一些答案.
Originaly posted on cakephp Q&A but i'll put it up here in hope of getting some answers.
我有很多公司的默认状态为 0,但有时会获得更高的状态.现在我想使用高状态(如果存在)但如果不存在则恢复为 0.我尝试了很多不同的方法,但我总是只得到状态为 0 的方法或具有我想要的状态的方法,如果存在则从不给我状态,如果不存在则为 0.
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 的状态:
Gives me only the status i specify, not giving me the ones with status 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
)
)
)
状态定义和代码检索数据:
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.
}
我为这个问题更改了模型的名称只是为了更有意义,当我告诉他们我为我的 flex 应用程序使用 cakephp 时,人们通常会感到害怕.我想这个问题的逻辑没有意义,但相信我,它在我的应用程序中是有意义的.
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.
谢谢!
推荐答案
我不确定是否理解您期望的结果.如果您想检索状态为 0 的所有记录,再加上状态为 3 的记录,您可以使用IN"而不是OR".
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'.
在 Cake 中,你可以这样写:
In Cake, you would write it like this:
$status = 3;
$conditions = array('Company.status' => array(0, $status));
这篇关于cakephp OR 条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:cakephp OR 条件
基础教程推荐
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01