Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Common/CommandData.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,5 +285,6 @@ private function getInputFromTable()
$tableName = $this->dynamicVars['$TABLE_NAME$'];

$this->inputFields = TableFieldsGenerator::generateFieldsFromTable($tableName);
$this->checkForDiffPrimaryKey();
}
}
28 changes: 27 additions & 1 deletion src/Utils/TableFieldsGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public static function generateFieldsFromTable($tableName)

$columns = $schema->listTableColumns($tableName);

$primaryKey = static::getPrimaryKeyFromTable($tableName);

$fields = [];

foreach ($columns as $column) {
Expand Down Expand Up @@ -78,13 +80,37 @@ public static function generateFieldsFromTable($tableName)
}

if (!empty($fieldInput)) {
$fields [] = GeneratorFieldsInputUtil::processFieldInput($fieldInput, $type, '', false);
$field = GeneratorFieldsInputUtil::processFieldInput($fieldInput, $type, '', false);

if ($column->getName() === $primaryKey) {
$field['primary'] = true;
$field['fillable'] = false;
}

$fields[] = $field;
}
}

return $fields;
}

/**
* @param string $tableName
*
* @return string|null The column name of the (simple) primary key
*/
public static function getPrimaryKeyFromTable($tableName)
{
$schema = DB::getDoctrineSchemaManager();
$indexes = collect($schema->listTableIndexes($tableName));

$primaryKey = $indexes->first(function ($i, $index) {
return $index->isPrimary() && 1 === count($index->getColumns());
});

return !empty($primaryKey) ? $primaryKey->getColumns()[0] : null;
}

/**
* @param string $name
* @param string $type
Expand Down