Use "()" in Fuelphp Query Builder
I would like to use "()" in a sql type sql file like this.
select * from shop where (item1=$item1 or item2=$item1) and flag=on;
I tried to express it like this:
$shop_query = DB::select()->from('shop');
$shop_query->where(function($shop_query){
$shop_query->where('item1','=',$item1)
->or_where('item2','=',$item1);
$shop_query ->and_where('flag','=','on');
However, this shows an error : undefined index item1.$item1
, and of course it matters.
How can I solve this?
+3
source to share
1 answer
You can use the grouping method of the ->where_open/close
query builder:
public static function whatever($item1, ... the rest of your args)
{
$shop_query = DB::select()
->from('shop')
->where('flag', 'on')
->and_where_open()
->where('item1', $item1)
->or_where('item2', $item1)
->and_where_close()
->execute()->as_array(); // just change this to whatever you need
return $shop_query;
}
This turns into:
SELECT * FROM `shop` WHERE `flag` = 'on' AND (`item1` = '$item1' OR `item2` = '$item1')
+6
source to share