From: Vincent Vanwaelscappel Date: Fri, 31 May 2019 15:43:56 +0000 (+0200) Subject: #2783 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=4ad60f0a753eaf9bd228e45b49dea95a869688a1;p=cubist_cms-back.git #2783 --- diff --git a/src/app/Magic/Fields/Field.php b/src/app/Magic/Fields/Field.php index d3a38f9..a67357e 100644 --- a/src/app/Magic/Fields/Field.php +++ b/src/app/Magic/Fields/Field.php @@ -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 index 0000000..18fc3ea --- /dev/null +++ b/src/app/Magic/Fields/SelectFromModelMultiple.php @@ -0,0 +1,15 @@ +_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; } }