How to make yii2 query using where() - andWhere()?
12,361
Solution 1
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();
Solution 2
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
Related videos on Youtube
![Bharat Chauhan](https://i.stack.imgur.com/M9cbF.jpg?s=256&g=1)
Author by
Bharat Chauhan
I am a Open Source developer. I have 5 years experience on php yii framework. I have powerful skills on Android + kotlin, Angular5, Node, Alexa, Typescript, Rxjs, Lodash js, PHP, Jquery, css, mysql, html.
Updated on July 01, 2022Comments
-
Bharat Chauhan almost 2 years
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
-
Beowulfenator over 8 yearsConsider using
andFilterWhere
. That way you don't even need to check if a variable is set:Student::find()->where(['status' => '1'])->andFilterWhere(['standard_id' => $params['standard_id'])
...
-