最初 发布 在 cakephp Q&A 上,但我会把它放在这里,希望能得到一些答案。
我有一堆公司的默认状态为 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 时,人们通常会害怕。我想这个问题的逻辑没有意义,但相信我,它在我的应用程序中是有意义的。
谢谢!
原文由 eldamar 发布,翻译遵循 CC BY-SA 4.0 许可协议
我不确定是否了解您期望的结果。如果要检索状态 = 0 的所有记录,再加上状态 = 3 的记录,则可以使用“IN”而不是“OR”。
在 Cake 中,你可以这样写: