最初发布在cakephp Q&A上,但我会把它放在这里,希望得到一些答案.
我有一些公司,其默认状态为0,但有时会获得更高的地位.现在我想使用高状态,如果存在,但如果没有,则恢复为0.我尝试了一堆不同的方法,但我总是得到状态为0的那些或者我想要的状态,如果存在则永远不给我状态,如果不存在则给0.
只给我指定的状态,而不是给我状态为0的状态:
'Company' => array (
'conditions' => array (
'OR' => array(
'Company.status' => 0,
'Company.status' => $status,
)
)
)
Run Code Online (Sandbox Code Playgroud)
只给我0的状态:
'Company' => array (
'conditions' => array (
'OR' => array(
'Company.status' => $status,
'Company.status' => 0
)
)
)
Run Code Online (Sandbox Code Playgroud)
状态定义和代码中的数据检索:
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.
}
Run Code Online (Sandbox Code Playgroud)
我更改了这个问题的模型的名称只是为了更有意义,当我告诉他们我使用cakephp进行我的flex应用时,人们通常会吓跑.我想这个问题的逻辑没有意义,但相信我在我的应用程序中有意义.
谢谢!
Vig*_*ond 50
尝试
'Company' => array (
'conditions' => array (
'OR' => array(
array('Company.status' => 0),
array('Company.status' => $status),
)
)
)
Run Code Online (Sandbox Code Playgroud)
如果它们属于相同的字段,它会在菜谱中包含或排列数组 http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#complex-find-conditions
nIc*_*IcO 26
我不确定你理解你期望的结果.如果要检索状态为0的所有记录,加上让我们说状态为3的记录,可以使用"IN"而不是"OR".
在Cake中,你会这样写:
$status = 3;
$conditions = array('Company.status' => array(0, $status));
Run Code Online (Sandbox Code Playgroud)