1
我打算有一個函數,將存儲sql語句在緩存上使用給定的第二個參數remember()
作爲關鍵和每當sql語句更改它將運行數據庫再次覆蓋存儲的sql,也包括緩存的結果,如果不存在,它將使用remember()
函數的默認緩存結果。如何擴展照明數據庫查詢生成器
所以我打算有這樣的事情上照亮\數據庫\查詢\生成器
/**
* 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,但問題是它沒有檢測到我的擴展類。
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;
我缺少的東西?
嗨,大家好, 我能通過在''上放置'newBaseQueryBuilder()'來擴展它ArdentBase'類。但是我沒有像我期望的那樣獲取Collection對象。我正在......'LaravelBook \ Ardent \ Builder'對象 – lukaserat