cakephp 2检索数据时的多个OR条件
cakephp 2 multiple OR conditions while retrieving data
我在我的查找函数中使用了多个 OR 条件,但 cakephp 覆盖了它并且只采用最新的 OR 条件,这非常令人不安。每个或条件侧重不同 table。这是我的自定义查找功能:
$studies = $this -> Study -> find('studyWithSequence',array(
'joins' => array(
array('table' => 'executed_study_tables', // Table name
'alias' => 'ExecutedStudyTable',
'type' => 'LEFT',
'conditions' => array(
'ExecutedStudyTable.study_id = Study.id', // Mention join condition here
)
)
),
'conditions' => array(
'Study.user_id' => $cId,
'OR'=>array(
array('Study.state'=>'active'),
array('Study.state'=>'rehearsal')
),
'SubjectFilter.studyCode'=>null,
'SubjectFilter.m'=>1,
'SubjectFilter.f'=>1,
'SubjectFilter.age18_24'=>1,
'SubjectFilter.age25_34'=>1,
'SubjectFilter.age35_44'=>1,
'SubjectFilter.age45_54'=>1,
'SubjectFilter.age55plus'=>1,
'OR'=>array(
'NOT'=>array(
array('ExecutedStudyTable.user_id'=>$user),
),
array('ExecutedStudyTable.user_id'=>null)
)
),
));
你知道如何同时保留两个或条件吗?
感谢您的回复
我找到了解决办法。
通常 cakephp 2 用 AND 连接所有条件,但在我的例子中你必须手动定义 AND 条件。像这样:
$studies = $this -> Study -> find('studyWithSequence',array(
'joins' => array(
array('table' => 'executed_study_tables',
'alias' => 'ExecutedStudyTable',
'type' => 'LEFT',
'conditions' => array(
'ExecutedStudyTable.study_id = Study.id',
)
)
),
'conditions' => array(
'Study.user_id' => $cId,
'OR'=>array(
array('Study.state'=>'active'),
array('Study.state'=>'rehearsal')
),
'SubjectFilter.studyCode'=>null,
'SubjectFilter.m'=>1,
'SubjectFilter.f'=>1,
'SubjectFilter.age18_24'=>1,
'SubjectFilter.age25_34'=>1,
'SubjectFilter.age35_44'=>1,
'SubjectFilter.age45_54'=>1,
'SubjectFilter.age55plus'=>1,
'AND'=>array(
'OR'=>array(
'NOT'=>array(
array('ExecutedStudyTable.user_id'=>$user),
),
array('ExecutedStudyTable.user_id'=>null)
)
)
),
));
我在我的查找函数中使用了多个 OR 条件,但 cakephp 覆盖了它并且只采用最新的 OR 条件,这非常令人不安。每个或条件侧重不同 table。这是我的自定义查找功能:
$studies = $this -> Study -> find('studyWithSequence',array(
'joins' => array(
array('table' => 'executed_study_tables', // Table name
'alias' => 'ExecutedStudyTable',
'type' => 'LEFT',
'conditions' => array(
'ExecutedStudyTable.study_id = Study.id', // Mention join condition here
)
)
),
'conditions' => array(
'Study.user_id' => $cId,
'OR'=>array(
array('Study.state'=>'active'),
array('Study.state'=>'rehearsal')
),
'SubjectFilter.studyCode'=>null,
'SubjectFilter.m'=>1,
'SubjectFilter.f'=>1,
'SubjectFilter.age18_24'=>1,
'SubjectFilter.age25_34'=>1,
'SubjectFilter.age35_44'=>1,
'SubjectFilter.age45_54'=>1,
'SubjectFilter.age55plus'=>1,
'OR'=>array(
'NOT'=>array(
array('ExecutedStudyTable.user_id'=>$user),
),
array('ExecutedStudyTable.user_id'=>null)
)
),
));
你知道如何同时保留两个或条件吗? 感谢您的回复
我找到了解决办法。 通常 cakephp 2 用 AND 连接所有条件,但在我的例子中你必须手动定义 AND 条件。像这样:
$studies = $this -> Study -> find('studyWithSequence',array(
'joins' => array(
array('table' => 'executed_study_tables',
'alias' => 'ExecutedStudyTable',
'type' => 'LEFT',
'conditions' => array(
'ExecutedStudyTable.study_id = Study.id',
)
)
),
'conditions' => array(
'Study.user_id' => $cId,
'OR'=>array(
array('Study.state'=>'active'),
array('Study.state'=>'rehearsal')
),
'SubjectFilter.studyCode'=>null,
'SubjectFilter.m'=>1,
'SubjectFilter.f'=>1,
'SubjectFilter.age18_24'=>1,
'SubjectFilter.age25_34'=>1,
'SubjectFilter.age35_44'=>1,
'SubjectFilter.age45_54'=>1,
'SubjectFilter.age55plus'=>1,
'AND'=>array(
'OR'=>array(
'NOT'=>array(
array('ExecutedStudyTable.user_id'=>$user),
),
array('ExecutedStudyTable.user_id'=>null)
)
)
),
));