]> _ Git - cubist_cms-back.git/commitdiff
wip #3753 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 26 Nov 2020 08:48:14 +0000 (09:48 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 26 Nov 2020 08:48:14 +0000 (09:48 +0100)
src/app/Magic/Fields/Model.php
src/app/Magic/Fields/ModelAttribute.php

index 1b502c4767ac6ec96bed6ad547fc972374b96d33..ea9b3edc50d6524c926e846c7f1c81ad2e4bcba4 100644 (file)
@@ -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);
index c459e3a61ececa1e1ee61b1486f748f3f9b9aee3..dc35e8cdc4677a475d957c86f95ed06141181ef8 100644 (file)
@@ -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];
+            }
+        }
     }
 }