]> _ Git - cubist_cms-back.git/commitdiff
wip #5041 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 9 Feb 2022 13:20:01 +0000 (14:20 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 9 Feb 2022 13:20:01 +0000 (14:20 +0100)
src/app/Magic/Fields/Model.php

index 25959058e6a2e16ab25a9e4ef05c1d2b3a510370..df3bb410753bb10baa6ec345c5d6a2fdfdb18fce 100644 (file)
@@ -10,7 +10,7 @@ class Model extends Field
 
     public function getDefaultAttributes()
     {
-        return array_merge(parent::getDefaultAttributes(), ['attribute' => 'name', 'column_attribute' => null, 'allows_null' => false, 'allows_multiple' => $this->_multiple, 'optionsmodel_global_scopes' => true]);
+        return array_merge(parent::getDefaultAttributes(), ['attribute' => 'name', 'optionsmodel_scope' => null, 'column_attribute' => null, 'allows_null' => false, 'allows_multiple' => $this->_multiple, 'optionsmodel_global_scopes' => true]);
     }
 
     public function _getOptions()
@@ -23,9 +23,10 @@ class Model extends Field
         $bui = backpack_user() === null ? '' : backpack_user()->id;
 
         $modelClass = $this->getAttribute('optionsmodel');
-        $globalCacheKey = '_getOption_Model_' . $modelClass . '-' . $attr;
-        $cacheKey = '_getOption_Model_' . $modelClass . '-' . $attr . '-' . $bui;
-        $cacheKeysKey = '_getOption_Model_' . $modelClass . '-Keys-' . $bui;
+        $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;
         if (isset(static::$_options[$cacheKey])) {
             return static::$_options[$cacheKey];
         }
@@ -33,11 +34,15 @@ 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) {
+            static::$_options[$globalCacheKey] = cache()->tags([$tag])->remember($globalCacheKey, 86400, function () use ($modelClass, $attr, $modelScope) {
                 set_time_limit(0);
                 /** @var \Illuminate\Database\Eloquent\Model $inst */
                 $inst = new $modelClass();
-                return $this->getClause($modelClass::withoutGlobalScopes())->get()->pluck($attr, $inst->getKeyName())->toArray();
+                $q = $this->getClause($modelClass::withoutGlobalScopes());
+                if ($modelScope) {
+                    $q = $q->$modelScope();
+                }
+                return $q->where('created_ok', 1)->get()->pluck($attr, $inst->getKeyName())->toArray();
             });
             stop_measure($globalCacheKey);
         }