Commit b21553c5 authored by beberlei's avatar beberlei

[2.0] - DDC-169 - Refactored lots of sections with duplicate code in gather...

[2.0] - DDC-169 - Refactored lots of sections with duplicate code in gather relations part of SchemaTool::getCreateSql()
parent 2c27435b
...@@ -328,41 +328,9 @@ class SchemaTool ...@@ -328,41 +328,9 @@ class SchemaTool
$foreignClass = $this->_em->getClassMetadata($mapping->targetEntityName); $foreignClass = $this->_em->getClassMetadata($mapping->targetEntityName);
if ($mapping->isOneToOne() && $mapping->isOwningSide) { if ($mapping->isOneToOne() && $mapping->isOwningSide) {
$options = array(); $primaryKeyColumns = $uniqueConstraints = array(); // unnecessary for this relation-type
$localColumns = array();
$foreignColumns = array();
foreach ($mapping->getJoinColumns() as $joinColumn) {
$columnName = $mapping->getQuotedJoinColumnName($joinColumn['name'], $this->_platform);
$referencedColumnName = $joinColumn['referencedColumnName'];
$referencedFieldName = $foreignClass->getFieldName($referencedColumnName);
if (!$foreignClass->hasField($referencedFieldName)) {
throw new \Doctrine\Common\DoctrineException(
"Column name `$referencedColumnName` referenced for relation from ".
"$mapping->sourceEntityName towards $mapping->targetEntityName does not exist."
);
}
$table->createColumn(
$columnName, $foreignClass->getTypeOfField($referencedFieldName), array('notnull' => false)
);
$localColumns[] = $columnName;
$foreignColumns[] = $joinColumn['referencedColumnName'];
if (isset($joinColumn['onUpdate'])) {
$options['onUpdate'] = $joinColumn['onUpdate'];
}
if (isset($joinColumn['onDelete'])) {
$options['onDelete'] = $joinColumn['onDelete'];
}
}
$table->addForeignKeyConstraint( $this->_gatherRelationJoinColumns($mapping->getJoinColumns(), $table, $foreignClass, $mapping, $primaryKeyColumns, $uniqueConstraints);
$foreignClass->getQuotedTableName($this->_platform),
$localColumns, $foreignColumns, null, $options
);
} else if ($mapping->isOneToMany() && $mapping->isOwningSide) { } else if ($mapping->isOneToMany() && $mapping->isOwningSide) {
//... create join table, one-many through join table supported later //... create join table, one-many through join table supported later
throw DoctrineException::notSupported(); throw DoctrineException::notSupported();
...@@ -370,87 +338,76 @@ class SchemaTool ...@@ -370,87 +338,76 @@ class SchemaTool
// create join table // create join table
$joinTable = $mapping->getJoinTable(); $joinTable = $mapping->getJoinTable();
$localColumns = array();
$foreignColumns = array();
$fkOptions = array();
$theJoinTable = $schema->createTable($mapping->getQuotedJoinTableName($this->_platform)); $theJoinTable = $schema->createTable($mapping->getQuotedJoinTableName($this->_platform));
$primaryKeyColumns = array(); $primaryKeyColumns = array();
$uniqueConstraints = array(); $uniqueConstraints = array();
foreach ($joinTable['joinColumns'] as $joinColumn) {
$columnName = $mapping->getQuotedJoinColumnName($joinColumn['name'], $this->_platform);
$theJoinTable->createColumn(
$columnName,
$class->getTypeOfColumn($joinColumn['referencedColumnName']),
array('notnull' => false)
);
$primaryKeyColumns[] = $columnName; // Build first FK constraint (relation table => source table)
$this->_gatherRelationJoinColumns($joinTable['joinColumns'], $theJoinTable, $class, $mapping, $primaryKeyColumns, $uniqueConstraints);
$localColumns[] = $columnName; // Build second FK constraint (relation table => target table)
$foreignColumns[] = $joinColumn['referencedColumnName']; $this->_gatherRelationJoinColumns($joinTable['inverseJoinColumns'], $theJoinTable, $foreignClass, $mapping, $primaryKeyColumns, $uniqueConstraints);
if(isset($joinColumn['unique']) && $joinColumn['unique'] == true) { foreach($uniqueConstraints AS $unique) {
$uniqueConstraints[] = array($joinColumn['name']); $theJoinTable->addUniqueIndex($unique);
} }
if (isset($joinColumn['onUpdate'])) { $theJoinTable->setPrimaryKey($primaryKeyColumns);
$fkOptions['onUpdate'] = $joinColumn['onUpdate'];
} }
if (isset($joinColumn['onDelete'])) {
$fkOptions['onDelete'] = $joinColumn['onDelete'];
} }
} }
// Build first FK constraint (relation table => source table) /**
$theJoinTable->addForeignKeyConstraint( * Gather columns and fk constraints that are required for one part of relationship.
$class->getQuotedTableName($this->_platform), $localColumns, $foreignColumns, null, $fkOptions *
); * @param array $joinColumns
* @param \Doctrine\DBAL\Schema\Table $theJoinTable
* @param ClassMetadata $class
* @param \Doctrine\ORM\Mapping\AssociationMapping $mapping
* @param array $primaryKeyColumns
* @param array $uniqueConstraints
*/
private function _gatherRelationJoinColumns($joinColumns, $theJoinTable, $class, $mapping, &$primaryKeyColumns, &$uniqueConstraints)
{
$localColumns = array(); $localColumns = array();
$foreignColumns = array(); $foreignColumns = array();
$fkOptions = array(); $fkOptions = array();
foreach ($joinTable['inverseJoinColumns'] as $inverseJoinColumn) { foreach ($joinColumns as $joinColumn) {
$primaryKeyColumns[] = $inverseJoinColumn['name']; $columnName = $mapping->getQuotedJoinColumnName($joinColumn['name'], $this->_platform);
$localColumns[] = $inverseJoinColumn['name'];
$foreignColumns[] = $inverseJoinColumn['referencedColumnName']; if (!$class->hasField($class->getFieldName($joinColumn['referencedColumnName']))) {
throw new \Doctrine\Common\DoctrineException(
"Column name `".$joinColumn['referencedColumnName']."` referenced for relation from ".
"$mapping->sourceEntityName towards $mapping->targetEntityName does not exist."
);
}
$primaryKeyColumns[] = $columnName;
$localColumns[] = $columnName;
$foreignColumns[] = $joinColumn['referencedColumnName'];
$theJoinTable->createColumn( $theJoinTable->createColumn(
$inverseJoinColumn['name'], $columnName, $class->getTypeOfColumn($joinColumn['referencedColumnName']), array('notnull' => false)
$this->_em->getClassMetadata($mapping->getTargetEntityName())
->getTypeOfColumn($inverseJoinColumn['referencedColumnName']),
array('notnull' => false)
); );
if(isset($inverseJoinColumn['unique']) && $inverseJoinColumn['unique'] == true) { if(isset($joinColumn['unique']) && $joinColumn['unique'] == true) {
$uniqueConstraints[] = array($inverseJoinColumn['name']); $uniqueConstraints[] = array($columnName);
} }
if (isset($inverseJoinColumn['onUpdate'])) { if (isset($joinColumn['onUpdate'])) {
$fkOptions['onUpdate'] = $inverseJoinColumn['onUpdate']; $fkOptions['onUpdate'] = $joinColumn['onUpdate'];
} }
if (isset($joinColumn['onDelete'])) { if (isset($joinColumn['onDelete'])) {
$fkOptions['onDelete'] = $inverseJoinColumn['onDelete']; $fkOptions['onDelete'] = $joinColumn['onDelete'];
}
} }
foreach($uniqueConstraints AS $unique) {
$theJoinTable->addUniqueIndex($unique);
} }
// Build second FK constraint (relation table => target table)
$theJoinTable->addForeignKeyConstraint( $theJoinTable->addForeignKeyConstraint(
$foreignClass->getQuotedTableName($this->_platform), $localColumns, $foreignColumns, null, $fkOptions $class->getQuotedTableName($this->_platform), $localColumns, $foreignColumns, null, $fkOptions
); );
$theJoinTable->setPrimaryKey($primaryKeyColumns);
}
}
} }
/** /**
......
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