Cakephp复杂查询结合条件

时间:2014-12-24 11:52:35

标签: php mysql cakephp

我在cakephp中有一些条件。例如,我需要检查状态= 1的表格,从='某个城市',年龄= 35和语言='英语'。我需要从表中获取所有条件的数据,例如状态和来自,状态和年龄,状态和语言,状态以及来自和年龄。对于我需要获取记录的条件的所有组合。请帮助解决此问题。我被困在这里2天了。提前致谢。我试过下面的查询

        if(!empty($search_terms['from'])){
            $conditions[] = array('location'=>$search_terms['from']);
        }
        if(!empty($search_terms['type'])){
            $conditions[] = array('maid_type'=>$search_terms['type']);
        }

        if(!empty($search_terms['age'])){
            if (strpos($search_terms['age'],'-') !== false) {
                $age_value = explode("-",$search_terms['age']);
                $conditions[] = array('AND'=>array(
                                            array('age <=' =>$age_value[0]),
                                            array('age >=' =>$age_value[1])));
            }

        }   

        if(!empty($search_terms['salary'])){
            if (strpos($search_terms['salary'],'-') !== false) {
                $salary_value = explode("-",$search_terms['salary']);
                $conditions[] = array('AND'=>array(
                                            array('salary >=' =>$salary_value[0]),
                                            array('salary <=' =>$salary_value[1])));
            }else{
                if (strpos($search_terms['salary'],'below') !== false){
                    $salary_value = filter_var($search_terms['salary'], FILTER_SANITIZE_NUMBER_INT);
                    $conditions[] = array('salary < '=> $salary_value);
                }elseif(strpos($search_terms['salary'],'upper') !== false){
                    $salary_value = filter_var($search_terms['salary'], FILTER_SANITIZE_NUMBER_INT);
                    $conditions[] = array('salary > '=> $salary_value);
                }
            }

        }                               

        if(!empty($search_terms['maritial'])){
            $conditions[] = array('maritial_status'=>$search_terms['maritial']);
        }                   

        if(!empty($search_terms['education'])){
            $conditions[] = array('education'=>$search_terms['education']);
        }                   

        if(!empty($search_terms['relegion'])){
            $conditions[] = array('relegion'=>$search_terms['relegion']);
        }       

        if(!empty($search_terms['duty_optn'])){
            foreach($search_terms['duty_optn'] as $duty_opn){
                $conditions[] = array('FIND_IN_SET(\''. $duty_opn .'\',maid_duty)');
            }
        }           

        if(!empty($search_terms['keyword'])){
            $conditions[] = array('code LIKE' => '%'.$search_terms['keyword'].'%');
        }   
    }


    $all_maids = $this->Maid->find('all',array('conditions'=>$conditions);

我得到以下条件

Array
(
    [0] => Array
        (
            [location] => indonesia
        )

    [1] => Array
        (
            [maid_type] => ex-singapore
        )

    [2] => Array
        (
            [AND] => Array
                (
                    [0] => Array
                        (
                            [age <=] => 41
                        )

                    [1] => Array
                        (
                            [age >=] => 50
                        )

                )

        )

    [3] => Array
        (
            [AND] => Array
                (
                    [0] => Array
                        (
                            [salary >=] => 400
                        )

                    [1] => Array
                        (
                            [salary <=] => 500
                        )

                )

        )

    [4] => Array
        (
            [maritial_status] => married
        )

    [5] => Array
        (
            [education] => high_school
        )

    [6] => Array
        (
            [relegion] => buddhist
        )

    [7] => Array
        (
            [0] => FIND_IN_SET('children_care',maid_duty)
        )

    [8] => Array
        (
            [0] => FIND_IN_SET('elderly_care',maid_duty)
        )

    [9] => Array
        (
            [0] => FIND_IN_SET('care_for_disabled',maid_duty)
        )

    [10] => Array
        (
            [code LIKE] => %code 3%
        )

)

1 个答案:

答案 0 :(得分:0)

plz以下面的格式创建你的数组:

阵 (         ['location'] =&gt; '印度尼西亚',         ['maid_type'] =&gt; “前新加坡,         ['age&lt; ='] =&gt; 41,         ['age&gt; ='] =&gt; 50,         ['salary&gt; ='] =&gt; 400,         ['salary&lt; ='] =&gt; 500,         ['maritial_status'] =&gt; '已婚',         ['education'] =&gt; '中学',             ['relegion'] =&gt; “佛教”,         [0] =&gt; FIND_IN_SET( 'children_care',maid_duty)         [1] =&gt; FIND_IN_SET( 'elderly_care',maid_duty)         [2] =&gt; FIND_IN_SET( 'care_for_disabled',maid_duty)         ['code LIKE'] =&gt; '%code 3%' )