2016-11-11 98 views
0

此查询并不按预期工作。会发生什么事情,它会选择与评估一词在其中的记录。我已经尝试了一些变化,但正如你所看到的,查询只能得到'数学'这个词的记录,并且不包含'奖学金'和'评估'的记录。在cakephp3中获取2条不同条件的记录

我不能得到这个在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']); 

回答

0

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

所以你havw使用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']);