在cakephp3中得到2条不符合条件的记录

时间:2016-11-11 04:29:08

标签: cakephp cakephp-3.0

此查询无法按预期工作。会发生什么情况会选择带有单词评估的记录。我尝试了一些变体,但正如您所看到的那样,查询只能获得带有“数学”字样的记录。用奖学金排除记录'和'评估'。

我无法在cakephp3中使用它。会发生什么事情,我得到记录的评估或奖学金。

$subjectMaths = $this->Students->Subjects->find('list')
    ->select(['id', 'name'])
    ->where([   
        'Subjects.name not LIKE'  => '%assessment%' ,
        'Subjects.name LIKE'  => '%maths%', 
        'Subjects.name not LIKE'  => '%scholarship%' 
    ])  
    ->order(['Subjects.name' => 'asc']);


$subjectMaths = $this->Students->Subjects->find('list')
    ->select(['id', 'name'])
    ->where([   
        'Subjects.name LIKE'  => '%maths%', 
        'OR'=> [
            ['Subjects.name not LIKE'  => '%assessment%' ],
            [ 'Subjects.name not LIKE'  => '%scholarship%',] 
        ]
    ])  
    ->order(['Subjects.name' => 'asc']);

1 个答案:

答案 0 :(得分:0)

这里的问题是你有两个相同的数组键Subjects.name not LIKE。第二个覆盖第一个

所以你有兴趣使用AndWhere()

$subjectMaths = $this->Students->Subjects->find('list')
    ->select(['id', 'name'])
    ->where(['Subjects.name not LIKE'  => '%assessment%'])
    ->andWhere(['Subjects.name LIKE'  => '%maths%']) 
    ->andWhere(['Subjects.name not LIKE'  => '%scholarship%']) 
    ->order(['Subjects.name' => 'asc']);