JOIN查询,在hasMany关联中建立条件

时间:2014-07-13 06:25:33

标签: cakephp inner-join has-many-through

我正在使用CakePHP 2.3.6。在一个项目中,我在hasMany关联中使用JOIN查询来搜索一些数据。我的情况应该是:

User.gender='male' AND User.country='USA'
AND
(Education.level='under_graduate' AND Education.result >= 3.5 AND Education.institute='college 1')
AND
(Education.level='graduate' AND Education.result >= 3 AND Education.institute='college 2')

我尝试了什么:

$this->paginate['User']['conditions']['AND']['User.gender']='male';
$this->paginate['User']['conditions']['AND']['User.country']='USA';
$this->paginate['User']['joins'][]=array('table'=>'educations','alias'=>'Education','type'=>'INNER','conditions'=>array('Education.user_id=User.id'));
$this->paginate['User']['conditions']['AND'][0]['AND']['Education.result >= ']=3.5;
$this->paginate['User']['conditions']['AND'][0]['AND']['Education.level']='under_graduate';
$this->paginate['User']['conditions']['AND'][0]['AND']['Education.institute']='college 1';
$this->paginate['User']['conditions']['AND'][1]['AND']['Education.result >= ']=3;
$this->paginate['User']['conditions']['AND'][1]['AND']['Education.level']='graduate';
$this->paginate['User']['conditions']['AND'][1]['AND']['Education.institute']='college 2';

我生成了这个条件数组:

[User] => Array
    (
        [conditions] => Array
            (
                [AND] => Array
                    (
                        [User.gender] => male
                        [User.country] => USA
                        [0] => Array
                            (
                                [AND] => Array
                                    (
                                        [Education.result >= ] => 3.5
                                        [Education.level] => under_graduate
                                        [Education.institute] => college 1
                                    )

                            )

                        [1] => Array
                            (
                                [AND] => Array
                                    (
                                        [Education.result >= ] => 3
                                        [Education.level] => graduate
                                        [Education.institute] => college 2
                                    )

                            )

                    )

            )

        [joins] => Array
            (
                [0] => Array
                    (
                        [table] => educations
                        [alias] => Education
                        [type] => INNER
                        [conditions] => Array
                            (
                                [0] => Education.user_id=User.id
                            )

                    )

            )

    )

但是,当我使用这个条件搜索时,它会返回一个空结果数组,但我知道数据库中有一些符合这些条件。

那么,这里有什么不对?我该怎么办 ?请帮帮我。

感谢。

2 个答案:

答案 0 :(得分:1)

您的查询正在使用所有AND语句,因此无法完成。 由于某个字段不能包含两个值,因此不会与Education.level = 'under_graduate''Education.level' = 'graduate'成一行。 您需要在查询中添加OR,例如:

User.gender='male' AND User.country='USA'
AND
(
    (Education.level='under_graduate' AND Education.result >= 3.5 AND Education.institute='college 1')
    OR
    (Education.level='graduate' AND Education.result >= 3 AND Education.institute='college 2')
)

我刚添加了一组额外的括号,并将与教育相关的两个条款之间的AND更改为OR。 希望您能够将其转换为CakePHP格式,它将起作用!

答案 1 :(得分:1)

所以我理解你需要找到一个有两种教育的用户。请参阅您在AND语句中的条件,您试图在同一时间找到等于“大学1”和“大学2”的Educations.institute,这是不可能的。因此,有一种方法可以通过为同一个表添加多个连接来找到它。像这样:

    $options['conditions']['User.gender']='male';
    $options['conditions']['User.country']='USA';
    $options['joins'][]=array(
        'table'=>'educations',
        'alias'=>'FirstEd',
        'type'=>'INNER',
        'conditions'=>array('FirstEd.user_id=User.id')
    );
    $options['joins'][]=array(
        'table'=>'educations',
        'alias'=>'SecondEd',
        'type'=>'INNER',
        'conditions'=>array('SecondEd.user_id=User.id')
    );
    $options['conditions']['AND'][0]['FirstEd.result']=3.5;
    $options['conditions']['AND'][0]['FirstEd.level'] = 'under_graduate';
    $options['conditions']['AND'][0]['FirstEd.institute']='college 1';
    $options['conditions']['AND'][1]['SecondEd.result']=3;
    $options['conditions']['AND'][1]['SecondEd.level']='graduate';
    $options['conditions']['AND'][1]['SecondEd.institute']='college 2';

    $this->Paginator->settings = $options;
    $users = $this->Paginator->paginate('User');

不是最好的解决方案,但它对我有用。