【问题标题】:FuelPHP Query builder join on multiple fieldsFuelPHP查询构建器加入多个字段
【发布时间】:2012-10-29 14:30:21
【问题描述】:

我需要使用各种参数的连接来创建查询,例如:

SELECT foo.*, bar.* FROM foo LEFT JOIN bar ON foo.c1 = bar.c1 AND foo.c2 = bar.c2 WHERE foo c3 = "somedata";

问题是我找不到如何使用 FuelPHP Query Builder 创建“AND”。

谢谢。

【问题讨论】:

  • 不知道 FuelPHP,但他们的文档清楚地展示了如何做到这一点。例如$result = DB::select()->from('foo')->join('bar','LEFT')->on('foo.c1', '=', 'foo.c1')->where('foo.c3', "somedata")->execute();

标签: php mysql sql fuelphp


【解决方案1】:

一个连接可以使用多个->on。在这种情况下,Builder 将使用“AND”组合它们。

【讨论】:

    【解决方案2】:

    他们编译 on 条件的方式使得无法设置像 status = 1 这样的值

    我将函数 on_string 添加到 \Fuel\Core\Database_Query_Builder_Join

    public function on_string($string) { 
        $this->_on_string = $string;
        return $this;
    }
    

    在编译()中

           if ($this->_on_string == "") { //yau add on string
            $conditions = array();
            foreach ($this->_on as $condition) {
                // Split the condition
                list($c1, $op, $c2) = $condition;
    
                if ($op) {
                    // Make the operator uppercase and spaced
                    $op = ' ' . strtoupper($op);
                }
    
                // Quote each of the identifiers used for the condition
                $conditions[] = $db->quote_identifier($c1) . $op . ' ' . $db->quote_identifier($c2);
            }
    
            // Concat the conditions "... AND ..."
            $sql .= '(' . implode(' AND ', $conditions) . ')';
        } else {            
            $sql .= '(' . $this->_on_string . ')'; //yau add on string
        }
    

    也要向 Database_Query_Builder_Select 公开您的方法。

        public function on_string($string)
    {
        $this->_last_join->on_string($string);
    
        return $this;
    }
    

    【讨论】:

      【解决方案3】:

      我认为这不是 Tan Shun Yau 所必需的。 您可以根据字符串进行选择,但您需要像这样转义值:

      ->on('t.object_property', '=', \DB::quote($property));

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-04-19
        • 1970-01-01
        • 2012-06-13
        • 1970-01-01
        • 2019-02-01
        • 1970-01-01
        • 2017-06-29
        • 2022-01-17
        相关资源
        最近更新 更多