How to use not equal to inside a Yii2 query - yii2

I want to use a yii2 query in which I want to check a not equal to condition.
I tried like this but it didn't give the desired results. How do I do it?
$details = MovieShows::find()
->where(['movie_id'=>$id])
->andWhere(['location_id'=>$loc_id])
->andWhere(['cancel_date'=>!$date])
->all();

In this case you need to use operator format: [operator, operand1, operand2, ...]. So your code should look like this:
$details = MovieShows::find()
->where(['movie_id'=>$id])
->andWhere(['location_id'=>$loc_id])
->andWhere(['<>','cancel_date', $date])
->all();
More about using where method and operator format

You can also try this:
$details = MovieShows::find()->where(['movie_id'=>$id])
->andWhere(['!=', 'cancel_date', $date])->all();
for Greater than
$details = MovieShows::find()->where(['movie_id'=>$id])
->andWhere(['>', 'cancel_date', $date])->all();
for less than
$details = MovieShows::find()->where(['movie_id'=>$id])
->andWhere(['<', 'cancel_date', $date])->all();
More check here

Maybe this one help...:)
$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))
]);

The better and safe way to apply a condition.
Booking::find()->where('tour_id = :tour_id and id != :id', ['tour_id'=> $chk->tour_id, 'id' => $id])->all();

You can use this
$this->find()->where(['resource_id' => $resource_id]) ->andWhere(['>=', 'start_time', $start_time])->all();

In addiction to the #tony answer, for those who need to encapsulate a subquery here there's a quick example:
$users = User::find()
->where([
'not in',
'id',
(new Query())
->select('user_id')
->from(MyTable::tableName())
->where(['related_id'=>$myRelatedId])
->all();

<?= $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')
Maybe it help you for whom need to use subquery .

In case anyone reading this needs to use a REGEXP or similar, this works in Yii2 (2.0.15.1):
->andWhere(['NOT REGEXP', 'column','[A-Za-z]'])

You can just use the below pasted code:
$details = MovieShows::find()->where(['movie_id'=>$id])
->andWhere(['location_id'=>$loc_id])
->andWhere(['not in','cancel_date',$date])->all();

Related

Yii2 Shorten where () and orWhere ()

References from here is there another way to Shorten where() and orWhere() ?
Example the code like :
$HrEmployeeShift_opt = ArrayHelper::map(
HrEmployeeShift::find()->where(['Status' => 'Pasif'])
->orWhere(['Status' => 'Rolling'])
->asArray()->all(), 'Id', 'Shift'
);
[UPDATE] SOLVED
Comparing same column for two values or more for the best solution in my opinion is to use IN Condition where we can clearly see the detailed code or you can also use OR or directly build it like ->where(['Status' => ['Pasif', 'Rolling']])
Code with IN :
$HrEmployeeShift_opt = ArrayHelper::map(
HrEmployeeShift::find()->where(
[
'IN',
'Status',['Pasif', 'Rolling']
]
)
->asArray()->all(), 'Id', 'Shift'
);
Since you are comparing same column for two values. I guess IN Condition will also wok.
HrEmployeeShift::find()
->where(['Status' => ['Pasif', 'Rolling']])
->asArray()
->all();
OR
HrEmployeeShift::find()
->where(['IN', 'Status', ['Pasif', 'Rolling']])
->asArray()
->all();
This will result in a condition Status IN ('Pasif', 'Rolling').
Posted from my mobile you are looking to do it the following way
$HrEmployeeShift_opt = ArrayHelper::map(
HrEmployeeShift::find()->where(
[
'OR',
['Status' => 'Pasif'],
['Status' => 'Rolling'],
]
)->asArray()->all(),
'Id',
'Shift'
);

YII2 extra condition with AND operator

I'm updating a record in this way:
Yii::$app->db->createCommand()->update('table', ['config' => json_encode($array)],
'field1 = :field1', [':field1' => $field1]
)->execute();
My aim is to add an extra condition with the AND operator but I don't know how to do it.
I've followed this example: LINK
// UPDATE (table name, column values, condition)
Yii::$app->db->createCommand()->update('user', ['status' => 1], 'age > 30')->execute();
But it doesn't show a lot of possibilities.
try this way
Yii::$app->db->createCommand()
->update('table', ['config' => json_encode($array)],
'field1 = :field1 AND field2 = :field2',[':field1' => $field1,':field2' => $field2])
->execute();
Just like an array, with each condition separated by a ,.
In your case:
Yii::$app->db->createCommand()->update(
'table',
['config' => json_encode($array)],
['field1' => $field1, 'field2' => $field2]
)->execute();
Note that with this syntax you don't need to bind params, you could specify them directly inside the array of conditions as Yii2 santizes them.

Need Help for build query with activedataprovider

I want to perform following WHERE condition in yii2 ActiveDataProvider
Expected Where Condition :
$query="WHERE VoterName Like '%s%' AND (contactno != '' OR whatsapp_no!= '')";
My current where Condition:
$query->andFilterWhere(['like', 'VoterName', $this->VoterName]);
$query->orWhere(['<>', 'contactno', ''])->orWhere(['<>', 'whatsapp_no', '']);
I want to fetch only those records who have contactno or whatsapp_no.
When you need to set multi condition, you must use andWhere, for example for your question:
$query->andFilterWhere(['like', 'VoterName', $this->VoterName]);
$query->andWhere(['OR',['<>', 'contactno', ''],['<>', 'whatsapp_no', '']]);
Reference: \yii\db\QueryInterface::where()
Can the following query meet your needs?
$query->andWhere(['like', 'VoterName', $this->VoterName])
->andWhere(['or',
['!=', 'contactno', ''],
['!=', 'whatsapp_no', '']
]);

How to make yii2 query using where() - andWhere()?

I have make one query using conditional statement.
$query = Student::find()
->where('status=1');
if(isset($params['standard_id']) AND !empty($params['standard_id'])){
$query->andWhere(["standard_id"=>$params['standard_id']]);
}
if(isset($params['section_id']) AND !empty($params['section_id'])){
$query->andWhere(["section_id"=>$params['section_id']]);
}
if(isset($params['year']) AND !empty($params['year'])){
$query->andWhere(["year"=>$params['year']]);
}
//$result = $query->all();
return $query;
Mysql
select *from student where satus=1 and standard_id=3 and section_id=1 and year=2015
If possible to make using where - andWhere in yii2?
please help me
Try this.can't understand your question clearly. may be this will help you.
$model = User::find()
->where('satus> :satus', [':satus' => '1'])
->andWhere('standard_id= :standard_id', [':standard_id' => 3])
->andWhere('section_id= :section_id', [':section_id' => 1])
->andWhere('year= :year', [':year' => 2015])
->all();
Yes, you can use both where() and andWhere(). This should helps you to understand how it works.
https://github.com/yiisoft/yii2/blob/master/framework/db/QueryTrait.php#L101

How to specify range in find() cakephp

I’m new to CakePHP and I’ve tried searching but I can’t find an answer to this question.
To put it simply, I want the query to be something like this:
SELECT id from posts WHERE id IN (15,18,20);
But I don’t know what to put in the find() call.
This is covered in the CakePHP online manual at http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#complex-find-conditions. Simply specify an array in your conditions:
<?php
$ids = array(1,2,3,4,5,6);
$results = $this->Post->find('all', array(
'conditions' => array(
'Post.id' => $ids
)
));
From the model it would be something like:
$ids = array(15, 18, 20);
$posts = $this->find('all', array(
'conditions' => array(
'Post.id' => $ids
)
);
in the conditions array, you can pass an array of values to be used in the 'IN' clause
From within the posts controller
$id_array = array(15, 18, 20);
$this->Post->find('all', array('Post.id' => $id_array));
More on the subject
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html
$conditions = array("Post.title" => array("First post", "Second post", "Third post"))
$this->find(all,array($conditions));
Chk it if it works.