首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何扩展Database\Database\Query\Builder

如何扩展Database\Database\Query\Builder
EN

Stack Overflow用户
提问于 2013-11-27 16:40:56
回答 1查看 1.1K关注 0票数 1

我计划有一个函数,它将使用remember()上给定的第二个参数作为键将sql语句存储在缓存中,每当sql语句更改时,它将再次针对数据库运行,并覆盖存储的sql,也覆盖缓存的结果,如果没有,它将通过remember()函数获取默认的缓存结果。

所以我计划在like \Database\Query\Builder上有类似这样的东西

代码语言:javascript
复制
/**
 * Execute the query based on the cached query
 *
 * @param  array  $columns
 * @return array|static[]
 */
public function getCacheByQuery($columns = array('*'))
{
    if ( ! is_null($this->cacheMinutes))
    {
        list($key, $minutes) = $this->getCacheInfo();

        // if the stored sql is the same with the new one then get the cached
        // if not, remove the cached query before calling the getCached
        $oldSql = self::flag($key);
        $newSql = $this->toSql().implode(',', $this->bindings);
        if ($newSql!==$oldSql)
        {
            // remove the cache
            \Cache::forget($key);
            // update the stored sql
            self::updateFlag($key, $newSql);
        }

        return $this->getCached($columns);

    }

    return $this->getFresh($columns);
}

public static function updateFlag($flag, $value)
{
    $flags = \Cache::get(t().'databaseFlags', []);
    $flags[$flag] = $value;
    \Cache::put(t().'databaseFlags',  $flags, USER_SESSION_EXPIRATION);
}

public static function flag($flag)
{
    $flags = \Cache::get(t().'databaseFlags', []);
    return @$flags[$flag] ?: false;
}

但问题是,我不想把它直接放在Database\Database\Query\Builder上,因为这只是我对我正在工作的当前应用程序的需要。我正在尝试扩展Illuminate\Database\Query\Builder,但问题是它没有检测到my extension类。

代码语言:javascript
复制
Call to undefined method Illuminate\Database\Query\Builder::getCachedByQuery()

我的扩展类

代码语言:javascript
复制
<?php namespace Lukaserat\Traits;

class QueryBuilder extends \Illuminate\Database\Query\Builder  {

    /**
     * Execute the query based on the caced query
     *
     * @param  array  $columns
     * @return array|static[]
     */
    public function getCachedByQuery($columns = array('*'))
    {
        if ( ! is_null($this->cacheMinutes))
        {
            list($key, $minutes) = $this->getCacheInfo();

            // if the stored sql is the same with the new one then get the cached
            // if not, remove the cached query before calling the getCached
            $oldSql = self::flag($key);
            $newSql = $this->toSql().implode(',', $this->bindings);
            if ($newSql!==$oldSql)
            {
                // remove the cache
                \Cache::forget($key);
                // update the stored sql
                self::updateFlag($key, $newSql);
            }

            return $this->getCached($columns);

        }

        return $this->getFresh($columns);
    }

    public static function updateFlag($flag, $value)
    {
        $flags = \Cache::get(t().'databaseFlags', []);
        $flags[$flag] = $value;
        \Cache::put(t().'databaseFlags',  $flags, USER_SESSION_EXPIRATION);
    }

    public static function flag($flag)
    {
        $flags = \Cache::get(t().'databaseFlags', []);
        return @$flags[$flag] ?: false;
    }


}

在..上实现

代码语言:javascript
复制
<?php
use LaravelBook\Ardent\Ardent;
use Lukaserat\Traits\DataTable;
use Lukaserat\Traits\QueryBuilder as QueryBuilder;
use Illuminate\Support\MessageBag as MessageBag;

class ArdentBase extends Ardent implements InterfaceArdentBase{
    use DataTable;

我是不是遗漏了什么?

EN

回答 1

Stack Overflow用户

发布于 2013-11-27 17:00:49

我通过将我在扩展类中创建的函数从getCachedByQuery重命名为get来覆盖Illuminate\Database\Query\Builder上的get()方法,因为我只是扩展了get的例程。

我变了

代码语言:javascript
复制
public function getCachedByQuery($columns = array('*'))

代码语言:javascript
复制
public function get()

在我的Lukaserat\Traits\QueryBuilder

现在它的运行情况和我预期的一样。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20237465

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档