/** * @param $model \Mindy\Orm\Model */ public function createTable(Model $model) { $columns = []; $command = $this->db->createCommand(); foreach ($model->getFieldsInit() as $name => $field) { if ($field->sqlType() !== false) { if (is_a($field, $model::$oneToOneField) && $field->reversed) { continue; } elseif (is_a($field, $model::$foreignField)) { $name .= "_id"; } $columns[$name] = $field->sql(); } else { if (is_a($field, $model::$manyToManyField)) { /* @var $field \Mindy\Orm\Fields\ManyToManyField */ if (!$this->hasTable($model, $field->getTableName())) { if ($field->through === null) { $command->createTable($field->getTableName(), $field->getColumns())->execute(); } } } } } $command->createTable($model->tableName(), $columns)->execute(); }
public function getSafeUp() { $lines = []; $added = []; $deleted = []; $fields = $this->getFields(); $lastMigrationFields = $this->getLastMigration(); foreach ($fields as $name => $field) { if (array_key_exists($name, $lastMigrationFields) === false) { $added[$name] = $field; } } if (!empty($lastMigrationFields)) { foreach ($lastMigrationFields as $name => $field) { if (array_key_exists($name, $fields) === false) { $deleted[$name] = $field; continue; } if ($field['hash'] == $fields[$name]['hash']) { continue; } if ($field['sqlType'] != $fields[$name]['sqlType']) { $lines[] = $this->space . '$this->alterColumn("' . $this->_model->tableName() . '", "' . $name . '", "' . $fields[$name]['sqlType'] . '");'; } elseif ($field['sqlType'] == $fields[$name]['sqlType'] && $fields['length'] != $fields[$name]['length']) { $lines[] = $this->space . '$this->alterColumn("' . $this->_model->tableName() . '", "' . $name . '", "' . $fields[$name]['sqlType'] . '");'; } } foreach ($deleted as $name => $field) { $lines[] = $this->space . '$this->dropColumn("' . $this->_model->tableName() . '", "' . $name . '");'; } } if (empty($lastMigrationFields)) { $columns = []; foreach ($this->_model->getFieldsInit() as $name => $field) { if ($field->sqlType() !== false) { if (is_a($field, ForeignField::className())) { $name .= "_id"; } $columns[$name] = $field->sql(); } else { if (is_a($field, ManyToManyField::className())) { /* @var $field \Mindy\Orm\Fields\ManyToManyField */ if ($field->through === null) { $lines[] = $this->space . '$this->createTable("' . $field->getTableName() . '", ' . $this->compileColumns($field->getColumns()) . ', null, true);'; } } } } $lines[] = $this->space . '$this->createTable("' . $this->_model->tableName() . '", ' . $this->compileColumns($columns) . ');'; } else { foreach ($added as $name => $field) { $lines[] = $this->space . '$this->addColumn("' . $this->_model->tableName() . '", "' . $name . '", "' . $field['sqlType'] . '");'; } } return implode("\n", $lines); }
public function getInfoFields(Model $model) { return array_keys($model->getFieldsInit()); }