/**
- * @var array
+ * @var Field[]
*/
protected $_relationships = [];
/** @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');
}
}
- protected function _addRelationship($field, $relationship)
+ /**
+ * @param $field Field
+ */
+ protected function _addRelationship($field)
{
- $this->_relationships[] = ['field' => $field, 'type' => $relationship];
+ $this->_relationships[] = $field;
}
public function generateCode()
}
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]);
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;
}
}