2016-11-11 100 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']);