具有条件组合的 Cakephp 复杂查询


Cakephp complex query with combination of conditions

我在 CakePHP 中有一些条件。 例如,我需要检查状态 = 1、from = '某个城市'、年龄 = 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%
        )
)

请按以下格式创建数组:

数组( ["位置"] => "印度尼西亚", ['maid_type'] => '前新加坡', ['年龄<='] => 41, ['年龄>='] => 50, ['工资>='] => 400, ['工资<='] => 500, ["maritial_status"] => "已婚", ["教育"] => "high_school", ["降级"] => "佛教徒", [0] => FIND_IN_SET('children_care',maid_duty), [1] => FIND_IN_SET('elderly_care',maid_duty), [2] => FIND_IN_SET('care_for_disabled',maid_duty), ['代码喜欢'] => '%代码 3%')