/** * For a given table, generate the migration object with the appropriate * parameters * @param string $tableName The name of the table * @param array $columns An array of SchemaExtractor Column objects */ private function createTableMigration($tableName, $columns) { // Get the parsed columns $parsedColumns = $this->schemaExtractor->extract($columns, $this->dbType); // Get the model name form the table name $modelName = ucwords(Pluralizer::singular($tableName)); // Create a new migration $this->migrationList->create($modelName, $tableName); // Now, proceed towards adding columns foreach ($parsedColumns as $column) { $type = $this->getLaravelColumnType($column); // For primary keys, we simply set pK if ($type == 'increments') { $this->migrationList->setPrimaryKey($modelName, $column->name); continue; } $c = array('name' => $column->name, 'type' => $type, 'parameters' => $this->getLaravelColumnParameters($column->parameters, $type), 'default' => is_null($column->defaultValue) ? '' : $column->defaultValue, 'unsigned' => $column->unsigned, 'nullable' => $column->null, 'primary' => $column->index == 'primary', 'unique' => $column->index == 'unique', 'index' => $column->index == 'multicolumn'); $this->migrationList->addColumn($modelName, $c); } }
/** * Add the necessary columns to the migration and functions to the models * for a polymorphic relation * @param array $rel An element of the relation array that is being processed */ private function processPolymorphicRelation($rel) { // Add in two columns to the related model // A foreign key is required to be specified in this case $this->migrationList->addForeignKey($rel['relatedModel'], $rel['fromModel'], $rel['foreignKey'] . '_id'); $this->migrationList->addForeignKey($rel['relatedModel'], $rel['fromModel'], $rel['foreignKey'] . '_type', 'string'); // Add functions in both the models // The relatedModel is where we need to add the morphTo function // with the same name as the foreignKey $this->modelList->addFunction($rel['relatedModel'], null, 'mt', $rel['foreignKey']); // Now add the appropriate function to the current table $this->modelList->addFunction($rel['fromModel'], $rel['relatedModel'], $rel['relationType'], $rel['foreignKey']); }