Skip to content
This repository has been archived by the owner on Aug 19, 2020. It is now read-only.

Commit

Permalink
Added checks for existing columns
Browse files Browse the repository at this point in the history
  • Loading branch information
aklinkert committed Jul 15, 2015
1 parent 67ff180 commit 89eafeb
Showing 1 changed file with 32 additions and 23 deletions.
55 changes: 32 additions & 23 deletions src/CompositeNumberRangeBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,29 +73,38 @@ public function modifyTable()
}
}

$foreignIdColumn = $table->addColumn(
array(
'name' => $foreignIdColumnName,
'type' => 'integer',
'required' => true
)
);

$compositeKeyForeignKeyName = $tableName . '_FK_' . $foreignIdColumnName;
$foreignKey = new ForeignKey($compositeKeyForeignKeyName);
$foreignKey->addReference($foreignIdColumnName, 'id');
$foreignKey->setForeignTableCommonName($foreignTableName);
$foreignKey->setOnUpdate(ForeignKey::CASCADE);
$foreignKey->setOnDelete(ForeignKey::CASCADE);
$table->addForeignKey($foreignKey);

$compositeKeyColumn = $table->addColumn(
array(
'name' => $compositeKeyColumnName,
'type' => 'integer',
'required' => false,
)
);
if ($table->hasColumn($foreignIdColumnName)) {
$foreignIdColumn = $table->getColumn($foreignIdColumnName);
} else {
$foreignIdColumn = $table->addColumn(
array(
'name' => $foreignIdColumnName,
'type' => 'integer',
'required' => true
)
);

$compositeKeyForeignKeyName = $tableName . '_FK_' . $foreignIdColumnName;
$foreignKey = new ForeignKey($compositeKeyForeignKeyName);
$foreignKey->addReference($foreignIdColumnName, 'id');
$foreignKey->setForeignTableCommonName($foreignTableName);
$foreignKey->setOnUpdate(ForeignKey::CASCADE);
$foreignKey->setOnDelete(ForeignKey::CASCADE);
$table->addForeignKey($foreignKey);

}

if ($table->hasColumn($compositeKeyColumnName)) {
$compositeKeyColumn = $table->getColumn($compositeKeyColumnName);
} else {
$compositeKeyColumn = $table->addColumn(
array(
'name' => $compositeKeyColumnName,
'type' => 'integer',
'required' => false,
)
);
}

$index = new Unique($tableName . '_UQ_' . $foreignIdColumnName . '_' . $compositeKeyColumnName);
$index->addColumn($foreignIdColumn);
Expand Down

0 comments on commit 89eafeb

Please sign in to comment.