如何在Laravel中扩展JoinClause类

时间:2018-12-17 15:06:48

标签: php mysql laravel php-7 laravel-5.7

我们想扩展Query \ Builder用于制定联接子句的JoinClause类。

我们已经像这样扩展了BaseModel的查询生成器:(实质上,CoreBaseModel只是具有一些额外功能的模型。)

abstract class BaseModel extends CoreBaseModel
{
    function newBaseQueryBuilder()
    {
        return new BaseModelQueryBuilder($this->getConnection());
    }
}

BaseModelQueryBuilder中的示例代码:

class BaseModelQueryBuilder extends Builder
{
    function findOverlapping($from, $till, $from_column, $till_column)
    {
        return $this
            ->orWhereBetween($from_column, [$from, $till])
            ->orWhereBetween($till_column, [$from, $till])
            ->orWhere(function($query) use($from_column, $till_column, $from, $till)
            {
                //Around
                $query
                    ->where($from_column, '<=' , $from)
                    ->where($till_column, '>=', $till);
            });
    }
}

这很好,因为您可以在该模型上的每个查询中以及每个子查询中都使用findOverlapping函数。

问题是这不适用于高级连接子句:

BaseModel::join('table_name', function($join)
{
    $join->where(function($query)
    {
        $query->findOverlapping('2018-01-01', '2018-21-31', 'from', 'till');
    });
});

错误是:

Call to undefined method Illuminate\Database\Query\JoinClause::findOverlapping()

因此,我一直在努力寻找解决此问题的方法,并在Illuminate\Database\Query\Builder上找到了如下所示的join函数:

/**
 * Add a join clause to the query.
 *
 * @param  string  $table
 * @param  \Closure|string  $first
 * @param  string|null  $operator
 * @param  string|null  $second
 * @param  string  $type
 * @param  bool    $where
 * @return $this
 */
public function join($table, $first, $operator = null, $second = null, $type = 'inner', $where = false)
{
    $join = new JoinClause($this, $type, $table);

    // If the first "column" of the join is really a Closure instance the developer
    // is trying to build a join with a complex "on" clause containing more than
    // one condition, so we'll add the join and call a Closure with the query.
    if ($first instanceof \Closure) {
        call_user_func($first, $join);

        $this->joins[] = $join;

        $this->addBinding($join->getBindings(), 'join');
    }

    // If the column is simply a string, we can assume the join simply has a basic
    // "on" clause with a single condition. So we will just build the join with
    // this simple join clauses attached to it. There is not a join callback.
    else {
        $method = $where ? 'where' : 'on';

        $this->joins[] = $join->$method($first, $operator, $second);

        $this->addBinding($join->getBindings(), 'join');
    }

    return $this;
}

因此我将函数复制到BaseModelQueryBuilder,制作了Illuminate\Database\Query\JoinClause类的副本,并将其分配给重写联接函数的第一条语句: $join = new BaseModelJoinClause($this, $type, $table)

请注意,与原始BaseModelJoinClause类相比,JoinClause类中的任何代码都没有更改(除了use语句和c的命名空间之外)。尽管如此,即使进行了这种细微的更改(仅分配了新类,它是原始类的完全相同),所有查询仍因以下错误而失败:SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax

如果有人对如何解决此问题有一些建议,或者有任何在laravel上扩展JoinClause类的经验,欢迎您提供帮助:)如果您认为需要更多信息,请告诉我。

0 个答案:

没有答案