From 3ef3b412d019a9771c9d4960c17c90a40e399ea3 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Fri, 31 May 2019 17:27:36 +0200 Subject: [PATCH] #2783 --- src/app/Magic/Fields/Field.php | 13 --- .../Magic/Models/CubistMagicAbstractModel.php | 81 +++++++++++++++++-- 2 files changed, 74 insertions(+), 20 deletions(-) diff --git a/src/app/Magic/Fields/Field.php b/src/app/Magic/Fields/Field.php index 1057560..afae70c 100644 --- a/src/app/Magic/Fields/Field.php +++ b/src/app/Magic/Fields/Field.php @@ -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') { - - } - } } diff --git a/src/app/Magic/Models/CubistMagicAbstractModel.php b/src/app/Magic/Models/CubistMagicAbstractModel.php index 9fed86e..c8f9ef5 100644 --- a/src/app/Magic/Models/CubistMagicAbstractModel.php +++ b/src/app/Magic/Models/CubistMagicAbstractModel.php @@ -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])) { -- 2.39.5