コード例 #1
0
 protected function renameOneToManyExtendField(Schema $schema, QueryBag $queries, Table $table, $associationName, $targetEntityClassName, EntityMetadataHelper $entityMetadataHelper)
 {
     $entityClassName = $entityMetadataHelper->getEntityClassByTableName($table->getName());
     $targetTableName = $entityMetadataHelper->getTableNameByEntityClass($targetEntityClassName);
     if ($schema->hasTable($targetTableName)) {
         $targetTable = $schema->getTable($targetTableName);
         $oldTargetColumnName = sprintf('field_%s_%s_id', strtolower(ExtendHelper::getShortClassName($entityClassName)), $associationName);
         if ($targetTable->hasColumn($oldTargetColumnName)) {
             $newTargetColumnName = $this->nameGenerator->generateOneToManyRelationColumnName($entityClassName, $associationName);
             $oldIndexName = $this->nameGenerator->generateIndexName($targetTableName, [$oldTargetColumnName], false, true);
             if ($targetTable->hasIndex($oldIndexName)) {
                 $targetTable->dropIndex($oldIndexName);
             }
             $oldForeignKeyName = $this->nameGenerator->generateForeignKeyConstraintName($targetTableName, [$oldTargetColumnName], true);
             if ($targetTable->hasForeignKey($oldForeignKeyName)) {
                 $targetTable->removeForeignKey($oldForeignKeyName);
             }
             $this->renameExtension->renameColumn($schema, $queries, $targetTable, $oldTargetColumnName, $newTargetColumnName);
             $this->renameExtension->addIndex($schema, $queries, $targetTable->getName(), [$newTargetColumnName]);
             $this->renameExtension->addForeignKeyConstraint($schema, $queries, $targetTable->getName(), $table->getName(), [$newTargetColumnName], $table->getPrimaryKeyColumns(), ['onDelete' => 'SET NULL']);
         }
     }
 }