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()
$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];
}
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);
}