/**
  * 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);
     }
 }
Esempio n. 2
0
 /**
  * Add the necessary columns to the migration and functions to the models
  * for a belongs to many relation, checking if a pivot table has to be created,
  * depending on whether the type is 'btm' or 'btmc'
  * @param  array $rel An element of the relation array that is being processed
  */
 private function processBelongsToManyRelation($rel)
 {
     // Determine if a pivot table has to be created
     $createTable = $rel['relationType'] == 'btm';
     // Determine the name of the pivot table
     // If we're given a pivot table override, use that
     if ($rel['pivotTable']) {
         $pivotTableName = $rel['pivotTable'];
     } else {
         $pivotTableName = strcmp($rel['fromModel'], $rel['relatedModel']) < 0 ? strtolower($rel['fromModel'] . '_' . $rel['relatedModel']) : strtolower($rel['relatedModel'] . '_' . $rel['fromModel']);
     }
     // Add in the new table if it is needed
     $createTable && $this->migrationList->create($pivotTableName, $pivotTableName);
     // Add in the columns
     // Take care to override column names if they have been provided
     if ($rel['foreignKey']) {
         $pivotColumn1 = $rel['foreignKey'][0];
         $pivotColumn2 = $rel['foreignKey'][1];
     } else {
         $pivotColumn1 = strtolower($rel['fromModel'] . '_id');
         $pivotColumn2 = strtolower($rel['relatedModel'] . '_id');
     }
     // If table is not need, make sure that the table already exists, and has
     // the necessary columns
     if (!$createTable) {
         // Check if the table exists
         if (!$this->migrationList->exists($pivotTableName)) {
             throw new ParseError("Custom pivot table '{$pivotTableName}' specified in model '" . $rel['fromModel'] . "'' does not exist.");
         }
         // Check if the columns exist
         $pivotMigration = $this->migrationList->get($pivotTableName);
         if (!$pivotMigration->columnExists($pivotColumn1)) {
             throw new ParseError("Custom pivot table '{$pivotTableName}' does not contain the necessary column: {$pivotColumn1}");
         }
         if (!$pivotMigration->columnExists($pivotColumn2)) {
             throw new ParseError("Custom pivot table '{$pivotTableName}' does not contain the necessary column: {$pivotColumn2}");
         }
         // Check column data types
         if (!($pivotMigration->getColumnType($pivotColumn1) == 'integer') or !$pivotMigration->isColumnUnsigned($pivotColumn1)) {
             throw new ParseError("Column '{$pivotColumn1}' in custom pivot table '{$pivotTableName}' needs to be of type unsigned integer");
         }
         if (!($pivotMigration->getColumnType($pivotColumn2) == 'integer') or !$pivotMigration->isColumnUnsigned($pivotColumn2)) {
             throw new ParseError("Column '{$pivotColumn2}' in custom pivot table '{$pivotTableName}' needs to be of type unsigned integer");
         }
     }
     // Then add in the two columns if a table has to be created
     $createTable && $this->migrationList->addForeignKey($pivotTableName, null, $pivotColumn1);
     $createTable && $this->migrationList->addForeignKey($pivotTableName, null, $pivotColumn2);
     // Manage the model function, which has to added regardless of whether
     // we're using a custom pivot or not
     // First, if we're using btmc, fetch additional columns that needs to go
     // with the pivot
     $additional = array('btmcColumns' => array(), 'btmcTimestamps' => false);
     if (!$createTable) {
         $pivotColumns = $this->migrationList->get($pivotTableName)->all();
         foreach ($pivotColumns as $name => $data) {
             // We should not add the pivot columns
             if (!in_array($name, array($pivotColumn1, $pivotColumn2))) {
                 $additional['btmcColumns'][] = $name;
             }
         }
         // Also check if timestamps are enabled
         if ($this->migrationList->get($pivotTableName)->timestamps) {
             $additional['btmcTimestamps'] = true;
         }
     }
     $this->modelList->addFunction($rel['fromModel'], $rel['relatedModel'], $rel['relationType'], $rel['foreignKey'], $rel['pivotTable'], $additional);
 }