use "()" in Fuelphp query builder

120 views Asked by At

I'd like to use "()" in fuelphp's sql 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 error: undefined index item1.$item1, and surely it has values.

How could I solve this?

1

There are 1 answers

1
Kevin On BEST ANSWER

You can use the grouping ->where_open/close method of the 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')