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