]> _ Git - cubist_cms-back.git/commitdiff
#2783
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 31 May 2019 15:27:36 +0000 (17:27 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Fri, 31 May 2019 15:27:36 +0000 (17:27 +0200)
src/app/Magic/Fields/Field.php
src/app/Magic/Models/CubistMagicAbstractModel.php

index 1057560c28cca1ac65ab6df6cf7a8f98fc42b81a..afae70c74729a7f8e33493295888993c895f434f 100644 (file)
@@ -126,17 +126,4 @@ class Field
             $table->addIndex([$name]);
         }
     }
-
-    public function relationship($type)
-    {
-        if ($type == 'hasOne') {
-
-        } else if ($type == "hasMany") {
-
-        } else if ($type == 'belongsTo') {
-
-        } else if ($type == 'belongsToMany') {
-
-        }
-    }
 }
index 9fed86e876fe916a7b87f1378c8b128e85d69338..c8f9ef5f3ef5143cff605b48cc75853ef1234d99 100644 (file)
@@ -153,7 +153,7 @@ class CubistMagicAbstractModel extends Model
 
     protected function _addRelationship($field, $relationship)
     {
-        $this->_relationships[] = ['field' => $field, 'relationship' => $relationship];
+        $this->_relationships[] = ['field' => $field, 'type' => $relationship];
     }
 
     public function generateCode()
@@ -227,14 +227,41 @@ class CubistMagicAbstractModel extends Model
         }
 
         if ($this->timestamps) {
-            $options = ['notnull' => false];
-            $table->addColumn(static::CREATED_AT, 'date', $options);
-            $table->addColumn(static::UPDATED_AT, 'date', $options);
-            $table->addColumn('deleted_at', 'date', $options);
+            $this->_addTimestampsDatabaseColumns($table);
         }
+
+        foreach ($this->_relationships as $relationship) {
+            if ($relationship['type'] === 'belongsToMany') {
+
+                $model=self::_toModel($field->getAttribute('model'));
+
+                /** @var Field $field */
+                $field = $relationship['field'];
+
+                $reltable = $schema->createTable($this->getRelationShipTable($relationship));
+                $reltable->addColumn('id', 'integer', ['autoincrement' => true, 'unsigned' => true]);
+                $reltable->setPrimaryKey(['id']);
+                $reltable->addColumn($this->getForeignKey(), 'integer', ['unsigned' => true]);
+                $reltable->addColumn($model->getForeignKey(), 'integer', ['unsigned' => true]);
+                $this->_addTimestampsDatabaseColumns($reltable);
+            }
+        }
+
         return $table;
     }
 
+
+    /**
+     * @param $table Table
+     */
+    protected function _addTimestampsDatabaseColumns($table)
+    {
+        $options = ['notnull' => false];
+        $table->addColumn(static::CREATED_AT, 'date', $options);
+        $table->addColumn(static::UPDATED_AT, 'date', $options);
+        $table->addColumn('deleted_at', 'date', $options);
+    }
+
     public function replicate(array $except = null)
     {
         $this->replicateSluggable($except);
@@ -243,15 +270,55 @@ class CubistMagicAbstractModel extends Model
     public function __call($method, $parameters)
     {
         foreach ($this->_relationships as $relationship) {
-            /** @var $relationship['field'] Field */
+            /** @var $relationship ['field'] Field */
             if ($method == $relationship['field']->getAttrbute('name')) {
-                return $relationship['field']->relationship();
+                return $this->relationship($relationship);
             }
         }
 
         return parent::__call($method, $parameters);
     }
 
+    public function relationship($relationship)
+    {
+        $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));
+        }
+    }
+
+    public function getRelationShipTable($relationship)
+    {
+        $type = $relationship['type'];
+        /** @var Field $field */
+        $field = $relationship['field'];
+
+        /** @var Model $foreignEntity */
+        $foreignEntity = self::_toModel($field->getAttribute('model'));
+
+        if ($type == 'belongsToMany') {
+            return 'rel_btm_' . $foreignEntity->table . '_' . $this->table;
+        }
+    }
+
+    /**
+     * @param $class Model|string
+     * @return Model
+     */
+    protected static function _toModel($class)
+    {
+        if (is_string($class) && class_exists($class)) {
+            return new $class();
+        }
+        if ($class instanceof Model) {
+            return $class;
+        }
+    }
+
     public static function toDoctrineType($type)
     {
         if (isset(self::$_doctrineTypesMapping[$type])) {