简体   繁体   English

在Fuelphp查询构建器中使用“()”

[英]use “()” in Fuelphp query builder

I'd like to use "()" in fuelphp's sql like this. 我想在fuelphp这样的sql中使用“()”。

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. 但是,这显示错误: undefined index item1.$item1 ,当然它有值。

How could I solve this? 我该怎么解决这个问题?

You can use the grouping ->where_open/close method of the query builder: 您可以使用查询构建器的分组->where_open/close方法:

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM