From 77ebebc5913f2c3513b18dfacdc33919764537f4 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 9 Feb 2022 14:20:01 +0100 Subject: [PATCH] wip #5041 @1 --- src/app/Magic/Fields/Model.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/app/Magic/Fields/Model.php b/src/app/Magic/Fields/Model.php index 2595905..df3bb41 100644 --- a/src/app/Magic/Fields/Model.php +++ b/src/app/Magic/Fields/Model.php @@ -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); } -- 2.39.5