From 6196856f9f509a1ec2b81ca5edf93fae34262dab Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 15 Jun 2022 11:47:50 +0200 Subject: [PATCH] wip #4210 @1 --- src/app/Magic/Fields/Model.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/app/Magic/Fields/Model.php b/src/app/Magic/Fields/Model.php index a9f1677..9425c5f 100644 --- a/src/app/Magic/Fields/Model.php +++ b/src/app/Magic/Fields/Model.php @@ -4,6 +4,8 @@ namespace Cubist\Backpack\Magic\Fields; +use Illuminate\Database\Query\Builder; + class Model extends Field { protected static $_options = []; @@ -28,9 +30,12 @@ class Model extends Field $modelClass = $this->getAttribute('optionsmodel'); $modelScope = $this->getAttribute('optionsmodel_scope', ''); - $globalCacheKey = '_getOption_Model_' . $modelClass . '-|' . $modelScope . '|-' . $attr; - $cacheKey = '_getOption_Model_' . $modelClass . '-' . $attr . '-|' . $modelScope . '|-' . $bui; - $cacheKeysKey = '_getOption_Model_' . $modelClass . '-Keys-|' . $modelScope . '|-' . $bui; + $modelOrderBy = $this->getAttribute('optionsmodel_orderby', ''); + $modelOrderByWay = $this->getAttribute('optionsmodel_orderby_way', 'asc'); + $baseCacheKey = '_getOption_Model_' . $modelClass . '-|' . $modelScope . '|-' . '!' . $modelOrderBy . '!' . $modelOrderByWay . '!!'; + $globalCacheKey = $baseCacheKey . '|-' . $attr; + $cacheKey = $globalCacheKey . '|-' . $bui; + $cacheKeysKey = $baseCacheKey . '/KEYS/' . '|-' . $bui; if (isset(static::$_options[$cacheKey])) { return static::$_options[$cacheKey]; } @@ -38,14 +43,18 @@ class Model extends Field if (!isset(static::$_options[$globalCacheKey])) { start_measure($globalCacheKey, 'Get options for model ' . $modelClass . ' / ' . $attr); - static::$_options[$globalCacheKey] = cache()->tags([$tag])->remember($globalCacheKey, 86400, function () use ($modelClass, $attr, $modelScope) { + static::$_options[$globalCacheKey] = cache()->tags([$tag])->remember($globalCacheKey, 86400, function () use ($modelClass, $attr, $modelScope, $modelOrderBy, $modelOrderByWay) { set_time_limit(0); /** @var \Illuminate\Database\Eloquent\Model $inst */ $inst = new $modelClass(); + /** @var Builder $q */ $q = $this->getClause($modelClass::withoutGlobalScopes()); if ($modelScope) { $q = $q->$modelScope(); } + if ($modelOrderBy) { + $q->orderBy($modelOrderBy, $modelOrderByWay); + } return $q->where('created_ok', 1)->get()->pluck($attr, $inst->getKeyName())->toArray(); }); stop_measure($globalCacheKey); -- 2.39.5