Reputation: 4504
Hi, my problem is simple i want to add multiple where condition on yii2 Query bulder, but i don't know what doing.
my code like that
public function searchForTrip($params, $extraParams, $filter) {
$query = Boatinformation::find()
->where(['what_island' => $params['BoatinformationSearch']['what_island']])
->all();
if(isset($filter)) {
foreach ($filter as $key => $value) {
->andFilterWhere([$value => 1])
}
}
return $query;
}
this is not working
please someone help me
Upvotes: 2
Views: 13771
Reputation: 133400
You can do in several way and you can found an useful guide in this doc http://www.yiiframework.com/doc-2.0/guide-db-query-builder.html
you can use string format specifying the condition as a string :
$query->where('your_field1 =1 and your_field2 = 2');
You can use hash format and
$query->where([
'your_field1' => 10,
'your_field2' => null,
'your_field3' => [4, 8, 15],
]);
You can use operator format
$query->where(['and' , 'your_field1', 10, ]);
and you can also use
andWhere('your_field'=> $your_value ),
Upvotes: 8
Reputation: 201
You should write something like
public function searchForTrip($params, $extraParams, $filter) {
$query = Boatinformation::find()
->where(['what_island' => $params['BoatinformationSearch']['what_island']])
if(isset($filter)) {
foreach ($filter as $key => $value) {
$query->andFilterWhere([$value => 1])
}
}
return $query;
}
$query->andFilterWhere()
add where condition if $value exist,
$query->andWhere()
add where condition always
and if you want to return objects instead of query you should return $query->all();
Upvotes: 6