cakephp 2检索数据时的多个OR条件

时间:2017-09-04 14:35:33

标签: cakephp cakephp-2.0

我在我的find函数中使用多个OR条件,但是cakephp会覆盖它并且仅采用最新的OR条件,这非常令人不安。每个或条件聚焦一个不同的表。这是我的自定义查找功能:

 $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)
            )                
        ),
    ));

你知道如何保持两者或条件吗? 感谢您的回复

1 个答案:

答案 0 :(得分:1)

我找到了解决方案。 通常,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)
            )         
            )       
        ),
    ));