Orm 如何扩展Illumb\Database\Query\Builder
我计划使用一个函数将sql语句存储在缓存中,使用memory上给定的第二个参数作为键,并且每当sql语句发生更改时,它将再次对数据库运行,并覆盖存储的sql和缓存结果,如果没有,则使用memory函数获取默认的缓存结果 因此,我计划在Illumb\Database\Query\Builder上安装类似的功能Orm 如何扩展Illumb\Database\Query\Builder,orm,laravel,extending-classes,Orm,Laravel,Extending Classes,我计划使用一个函数将sql语句存储在缓存中,使用memory上给定的第二个参数作为键,并且每当sql语句发生更改时,它将再次对数据库运行,并覆盖存储的sql和缓存结果,如果没有,则使用memory函数获取默认的缓存结果 因此,我计划在Illumb\Database\Query\Builder上安装类似的功能 /** * Execute the query based on the cached query * * @param array $columns * @return arr
/**
* 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;
}
但问题是,我不想把它直接放在Illumb\Database\Query\Builder上,因为它只是我当前正在工作的应用程序的需要。我正在尝试扩展Illumb\Database\Query\Builder,但问题是它没有检测到my扩展类
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;
我遗漏了什么吗?因为我刚刚扩展了get的例程,所以我通过从getCachedByQuery将扩展类中创建的函数重命名为get,覆盖了illumb\Database\Query\Builder上的get方法是否正确 我变了
public function getCachedByQuery($columns = array('*'))
到
在我的Lukaserat\Traits\QueryBuilder上
现在它正如我所期望的那样工作。大家好,我能够通过在ArdentBase类上添加一个newBaseQueryBuilder的副本来扩展它。但是我没有得到我期望的收集对象。我要。。LaravelBook\Ardent\Builder对象
public function get()