Commit e5c2695d authored by Eugene Pisarchik's avatar Eugene Pisarchik

refactored getColumns method

parent 87251a14
...@@ -742,7 +742,7 @@ class MySqlPlatform extends AbstractPlatform ...@@ -742,7 +742,7 @@ class MySqlPlatform extends AbstractPlatform
foreach ($diff->changedIndexes as $changedIndex) { foreach ($diff->changedIndexes as $changedIndex) {
// Changed primary key // Changed primary key
if ($changedIndex->isPrimary() && $diff->fromTable instanceof Table) { if ($changedIndex->isPrimary() && $diff->fromTable instanceof Table) {
foreach ($diff->fromTable->getPrimaryKeyColumns() as $columnName) { foreach ($diff->fromTable->getPrimaryKeyColumnNames() as $columnName) {
$column = $diff->fromTable->getColumn($columnName); $column = $diff->fromTable->getColumn($columnName);
// Check if an autoincrement column was dropped from the primary key. // Check if an autoincrement column was dropped from the primary key.
......
...@@ -616,29 +616,63 @@ class Table extends AbstractAsset ...@@ -616,29 +616,63 @@ class Table extends AbstractAsset
} }
/** /**
* Returns ordered list of columns (primary keys are first, then foreign keys, then the rest)
* @return Column[] * @return Column[]
*/ */
public function getColumns() public function getColumns()
{ {
$columns = $this->_columns; return array_merge($this->getPrimaryKeyColumns(), $this->getForeignKeyColumns(), $this->_columns);
}
$pkCols = array(); /**
$fkCols = array(); * Returns primary key columns
* @return Column[]
*/
public function getPrimaryKeyColumns()
{
$primaryKeyColumnNames = $this->getPrimaryKeyColumnNames();
return array_filter($this->_columns, function ($key) use ($primaryKeyColumnNames) {
return in_array($key, $primaryKeyColumnNames);
}, ARRAY_FILTER_USE_KEY);
}
/**
* Returns foreign key columns
* @return Column[]
*/
public function getForeignKeyColumns()
{
$foreignKeyColumnNames = $this->getForeignKeyColumnNames();
return array_filter($this->_columns, function ($key) use ($foreignKeyColumnNames) {
return in_array($key, $foreignKeyColumnNames);
}, ARRAY_FILTER_USE_KEY);
}
/**
* Returns primary key column names
* @return array
*/
public function getPrimaryKeyColumnNames()
{
$primaryKeyColumnNames = [];
if ($this->hasPrimaryKey()) { if ($this->hasPrimaryKey()) {
$pkCols = $this->getPrimaryKey()->getColumns(); $primaryKeyColumnNames = $this->getPrimaryKey()->getColumns();
} }
foreach ($this->getForeignKeys() as $fk) { return $primaryKeyColumnNames;
/* @var $fk ForeignKeyConstraint */ }
$fkCols = array_merge($fkCols, $fk->getColumns());
}
$colNames = array_unique(array_merge($pkCols, $fkCols, array_keys($columns)));
uksort($columns, function ($a, $b) use ($colNames) {
return (array_search($a, $colNames) >= array_search($b, $colNames));
});
return $columns; /**
* Returns foreign key column names
* @return array
*/
public function getForeignKeyColumnNames()
{
$foreignKeyColumnNames = [];
foreach ($this->getForeignKeys() as $foreignKey) {
/* @var $foreignKey ForeignKeyConstraint */
$foreignKeyColumnNames = array_merge($foreignKeyColumnNames, $foreignKey->getColumns());
}
return $foreignKeyColumnNames;
} }
/** /**
...@@ -688,22 +722,6 @@ class Table extends AbstractAsset ...@@ -688,22 +722,6 @@ class Table extends AbstractAsset
return $this->getIndex($this->_primaryKeyName); return $this->getIndex($this->_primaryKeyName);
} }
/**
* Returns the primary key columns.
*
* @return array
*
* @throws DBALException
*/
public function getPrimaryKeyColumns()
{
if ( ! $this->hasPrimaryKey()) {
throw new DBALException("Table " . $this->getName() . " has no primary key.");
}
return $this->getPrimaryKey()->getColumns();
}
/** /**
* Returns whether this table has a primary key. * Returns whether this table has a primary key.
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment