]> _ Git - cubist_cms-back.git/commitdiff
#2783
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 31 May 2019 15:43:56 +0000 (17:43 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 31 May 2019 15:43:56 +0000 (17:43 +0200)
src/app/Magic/Fields/Field.php
src/app/Magic/Fields/SelectFromModelMultiple.php [new file with mode: 0644]
src/app/Magic/Models/CubistMagicAbstractModel.php

index d3a38f9df77e9e883474c18fd4e15b4dbe7c1b12..a67357e9e7b8c42c851b60234b2c85edfdc0ae50 100644 (file)
@@ -127,6 +127,14 @@ class Field
         }
     }
 
+    /**
+     * @return null|string
+     */
+    public function getRelationship()
+    {
+        return null;
+    }
+
     protected function _postSetAttributes()
     {
 
diff --git a/src/app/Magic/Fields/SelectFromModelMultiple.php b/src/app/Magic/Fields/SelectFromModelMultiple.php
new file mode 100644 (file)
index 0000000..18fc3ea
--- /dev/null
@@ -0,0 +1,15 @@
+<?php
+
+
+namespace Cubist\Backpack\app\Magic\Fields;
+
+
+class SelectFromModelMultiple extends SelectFromModel
+{
+    protected $_adminType = 'select2_multiple';
+
+    public function getRelationship()
+    {
+        return 'belongsToMany';
+    }
+}
index 4294e1400ad030ff7b59024d6f707ee458281561..e3dcda9bebd317d20e993fc8f1ce355e0ed90aba 100644 (file)
@@ -51,7 +51,7 @@ class CubistMagicAbstractModel extends Model
 
 
     /**
-     * @var array
+     * @var Field[]
      */
     protected $_relationships = [];
 
@@ -131,8 +131,8 @@ class CubistMagicAbstractModel extends Model
 
         /** @var Field $field */
         $field = Field::getInstance($attributes);
-        if (isset($attributes['relationship'])) {
-            $this->_addRelationship($field, $attributes['relationship']);
+        if (null !== $field->getRelationship()) {
+            $this->_addRelationship($field);
         }
 
         $name = $field->getAttribute('name');
@@ -151,9 +151,12 @@ class CubistMagicAbstractModel extends Model
         }
     }
 
-    protected function _addRelationship($field, $relationship)
+    /**
+     * @param $field Field
+     */
+    protected function _addRelationship($field)
     {
-        $this->_relationships[] = ['field' => $field, 'type' => $relationship];
+        $this->_relationships[] = $field;
     }
 
     public function generateCode()
@@ -231,12 +234,9 @@ class CubistMagicAbstractModel extends Model
         }
 
         foreach ($this->_relationships as $relationship) {
-            if ($relationship['type'] === 'belongsToMany') {
+            if ($relationship->getRelationship() === 'belongsToMany') {
 
-                $model=self::_toModel($field->getAttribute('model'));
-
-                /** @var Field $field */
-                $field = $relationship['field'];
+                $model = self::_toModel($relationship->getAttribute('model'));
 
                 $reltable = $schema->createTable($this->getRelationShipTable($relationship));
                 $reltable->addColumn('id', 'integer', ['autoincrement' => true, 'unsigned' => true]);
@@ -279,28 +279,30 @@ class CubistMagicAbstractModel extends Model
         return parent::__call($method, $parameters);
     }
 
-    public function relationship($relationship)
+    /**
+     * @param $field Field
+     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo|\Illuminate\Database\Eloquent\Relations\BelongsToMany
+     */
+    public function relationship($field)
     {
-        $type = $relationship['type'];
-        /** @var Field $field */
-        $field = $relationship['field'];
-        if ($type == 'belongsTo') {
-            return $this->belongsTo($field->getAttribute('model'), $field->getAttribute('name'));
-        } else if ($type == 'belongsToMany') {
-            return $this->belongsToMany($field->getAttribute('model'), $this->getRelationShipTable($relationship));
+        switch ($field->getRelationship()) {
+            case 'belongsTo':
+                return $this->belongsTo($field->getAttribute('model'), $field->getAttribute('name'));
+            case 'belongsToMany':
+                return $this->belongsToMany($field->getAttribute('model'), $this->getRelationShipTable($field));
         }
     }
 
-    public function getRelationShipTable($relationship)
+    /**
+     * @param $field Field
+     * @return string
+     */
+    public function getRelationShipTable($field)
     {
-        $type = $relationship['type'];
-        /** @var Field $field */
-        $field = $relationship['field'];
-
         /** @var Model $foreignEntity */
         $foreignEntity = self::_toModel($field->getAttribute('model'));
 
-        if ($type == 'belongsToMany') {
+        if ($field->getRelationship() == 'belongsToMany') {
             return 'rel_btm_' . $foreignEntity->table . '_' . $this->table;
         }
     }