namespace Cubist\Backpack\Magic\Fields;
+use Illuminate\Database\Query\Builder;
+
class Model extends Field
{
protected static $_options = [];
$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];
}
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);