【问题标题】:use "()" in Fuelphp query builder在 Fuelphp 查询生成器中使用“()”
【发布时间】:2015-01-10 08:11:36
【问题描述】:

我想像这样在fuelphp的sql中使用“()”。

select * from shop where (item1=$item1 or item2=$item1) and flag=on;

我试着这样表达;

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

但是,这显示错误:undefined index item1.$item1,而且肯定有值。

我该如何解决这个问题?

【问题讨论】:

    标签: php fuelphp


    【解决方案1】:

    您可以使用查询生成器的分组->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; 
    }
    

    这变成:

    SELECT * FROM `shop` WHERE `flag` = 'on' AND (`item1` = '$item1' OR `item2` = '$item1')
    

    【讨论】:

    • @user3119018 哦,我之前回答了你的一个问题:D。肯定有人很高兴这有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 2019-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多