From 89eafebb3dab2edbe7b0ba8185e35d4c737d8779 Mon Sep 17 00:00:00 2001 From: Alexander Pinnecke Date: Wed, 15 Jul 2015 20:44:03 +0200 Subject: [PATCH] Added checks for existing columns --- src/CompositeNumberRangeBehavior.php | 55 ++++++++++++++++------------ 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/src/CompositeNumberRangeBehavior.php b/src/CompositeNumberRangeBehavior.php index fc9af32..472608e 100644 --- a/src/CompositeNumberRangeBehavior.php +++ b/src/CompositeNumberRangeBehavior.php @@ -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);