From efb3c38832701fcbad9f19ee6ec3f827f337838c Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Thu, 26 Nov 2020 09:48:14 +0100 Subject: [PATCH] wip #3753 @0.5 --- src/app/Magic/Fields/Model.php | 11 ++++++----- src/app/Magic/Fields/ModelAttribute.php | 18 +++++++++++++++++- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/app/Magic/Fields/Model.php b/src/app/Magic/Fields/Model.php index 1b502c4..ea9b3ed 100644 --- a/src/app/Magic/Fields/Model.php +++ b/src/app/Magic/Fields/Model.php @@ -14,22 +14,23 @@ class Model extends Field } protected function _getOptions() + { + return $this->pluck($this->getAttribute('attribute')); + } + + public function pluck($attr) { $modelClass = $this->getAttribute('optionsmodel'); - $attr = $this->getAttribute('attribute'); $cacheKey = '_getOption_Model_' . $modelClass . '-' . $attr; - if (isset(static::$_options[$cacheKey])) { return static::$_options[$cacheKey]; } - start_measure($cacheKey, 'Get options for model ' . $modelClass . ' / ' . $attr); - $tag='model_' . $modelClass; + $tag = 'model_' . $modelClass; static::$_options[$cacheKey] = cache()->tags([$tag])->remember($cacheKey, 86400, function () use ($modelClass, $attr) { /** @var \Illuminate\Database\Eloquent\Model $inst */ $inst = new $modelClass(); $options = $modelClass::all(); - return $options->pluck($attr, $inst->getKeyName())->toArray(); }); stop_measure($cacheKey); diff --git a/src/app/Magic/Fields/ModelAttribute.php b/src/app/Magic/Fields/ModelAttribute.php index c459e3a..dc35e8c 100644 --- a/src/app/Magic/Fields/ModelAttribute.php +++ b/src/app/Magic/Fields/ModelAttribute.php @@ -4,10 +4,26 @@ namespace Cubist\Backpack\app\Magic\Fields; +use Cubist\Backpack\app\Magic\Models\CubistMagicAbstractModel; + class ModelAttribute extends StaticValue { public function filterValue($value) { - $a = $this->getAttribute('attributes'); + $a = $this->getAttribute('attribute'); + $e = explode('.', $a); + + $i = $this->getModelInstance(); + $v0 = $i->getAttribute($e[0]); + if (count($e) === 1) { + return $v0; + } + + if ($i instanceof CubistMagicAbstractModel) { + $f = $i->getField($e[0]); + if ($f instanceof Model) { + return $f->pluck($e[1])[$v0]; + } + } } } -- 2.39.5