]> _ Git - cubist_cms-back.git/commitdiff
wip #4210 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 15 Jun 2022 09:47:50 +0000 (11:47 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 15 Jun 2022 09:47:50 +0000 (11:47 +0200)
src/app/Magic/Fields/Model.php

index a9f1677f662279e67f04f2f9f7c7891dc5631f5f..9425c5fe7c5a7510fbae66d8b9e4fa06e0e382cd 100644 (file)
@@ -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);