如何扩展Illuminate \ Database \ Query \ Builder

时间:2013-11-27 08:40:56

标签: orm laravel extending-classes

我打算使用remember()上的给定第二个参数作为密钥,将一个函数存储在缓存中,每当sql语句更改时,它将再次针对数据库运行,覆盖存储的sql,也是缓存的结果,如果不是,它将采用remember()函数的默认缓存结果。

所以我计划在Illuminate \ Database \ Query \ Builder

上安装类似的东西
/**
 * 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;
}

但问题是,我不想直接将它放在Illuminate \ Database \ Query \ Builder上,因为这只是我对当前正在使用的应用程序的需求。我试图扩展Illuminate \ Database \ Query \ Builder,但问题是它没有检测到我的扩展类。

Call to undefined method Illuminate\Database\Query\Builder::getCachedByQuery()

我的分机课程

<?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;
    }


}

实施..

<?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;

我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

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

我改变了

public function getCachedByQuery($columns = array('*'))

public function get()

Lukaserat\Traits\QueryBuilder

它现在按照我的预期工作..