2015-06-25 20 views
30

Chcę użyć kwerendy yii2, w którym chcę sprawdzić nie równa warunku. Próbowałem tak, ale nie dało to pożądanych rezultatów. jak mam to zrobić?Jak używać nie równa się kwerendy Yii2

$details  = MovieShows::find()->where(['movie_id'=>$id]) 
        ->andWhere(['location_id'=>$loc_id]) 
        ->andWhere(['cancel_date'=>!$date])->all(); 
+4

Dlaczego po prostu nie patrzeć na dokumenty zamiast takich zgadywanek? – arogachev

+0

http://stackoverflow.com/a/32089897/2559712 – ankitr

Odpowiedz

67

W tym przypadku należy użyć formatu operatora: [operator, operand1, operand2, ...]. Więc kod powinien wyglądać tak:

$details = MovieShows::find()->where(['movie_id'=>$id]) 
      ->andWhere(['location_id'=>$loc_id]) 
      ->andWhere(['<>','cancel_date', $date])->all(); 

More o używaniu gdzie metoda i format operator

+7

Możesz również użyć '! =' Jako operatora "nie równego". – arogachev

6

Im lepszy i bezpieczny sposób, aby zastosować warunek.

Booking::find()->where('tour_id = :tour_id and id != :id', ['tour_id'=> $chk->tour_id, 'id' => $id])->all(); 
+0

Tak bezpieczny, jak inne odpowiedzi, ale bardziej przejrzysty i prosty –

10

Można też spróbować:

$details = MovieShows::find()->where(['movie_id'=>$id]) 
      ->andWhere(['!=', 'cancel_date', $date])->all(); 

przez ponad

$details = MovieShows::find()->where(['movie_id'=>$id]) 
       ->andWhere(['>', 'cancel_date', $date])->all(); 

za mniej niż

$details = MovieShows::find()->where(['movie_id'=>$id]) 
       ->andWhere(['<', 'cancel_date', $date])->all(); 

More sprawdzanie

5

Może ten pomaga ... :)

$query->andFilterWhere([ 'or', 
          ['<=', 'affidavit_cont', 0], 
          ['=', 'affidavit_cont1', 0], 
          ['>', 'affidavit_cont2', 0], 
          ['!=', 'affidavit_cont3', $this->affidavit3], 
          ['in', 'attempt_count', $this->attempted], 

         ]); 

$query->andFilterWhere(['like', 'job_invoice.paid_status', '',]); 

$query->andFilterWhere(['in', 'status_id', $this->status_id]); 

$query->andFilterWhere(['between', 'last_attempt', 
    strtotime(Utility::convertDate2Db($this->from_date)), 
    strtotime(Utility::convertDate2Db($this->to_date)) 
    ]); 
0

można po prostu użyć poniżej wklejony kod:

$details  = MovieShows::find()->where(['movie_id'=>$id]) 
        ->andWhere(['location_id'=>$loc_id]) 
        ->andWhere(['not in','cancel_date',$date])->all(); 
1

W uzależnieniem odpowiedź @tony, dla tych, którzy potrzebują do hermetyzacji podzapytanie tutaj jest szybkie przykład:

$users = User::find()     
      ->where([ 
       'not in', 
       'id', 
       (new Query()) 
        ->select('user_id') 
        ->from(MyTable::tableName()) 
        ->where(['related_id'=>$myRelatedId]) 
      ->all(); 
2

można użyć tej

$this->find()->where(['resource_id' => $resource_id]) ->andWhere(['>=', 'start_time', $start_time])->all(); 
0
 <?= $form->field($model, 'first_name')->dropDownList(
     arrayhelper::map(user::find() 
           ->where([ 'not in ' ,'first_name', patient::find() ->select([ 'patient_name' ]) ->asArray() ]) 
     ->all(),'id','first_name') 

Może to pomóc, dla których trzeba użyć podkwerenda.