Neos\Flow\Persistence\Doctrine\Service::getForeignKeyHandlingSql PHP Метод

getForeignKeyHandlingSql() публичный статический Метод

For a column that is used in a FK constraint to be renamed, the FK constraint has to be dropped first, then the column can be renamed and last the FK constraint needs to be added back (using the new name, of course). This method helps with the task of handling the FK constraints during this. Given a list of tables that contain columns to be renamed and a search/replace pair for the column name, it will return an array with arrays with drop and add SQL statements. Use them like this before and after renaming the affected fields: collect foreign keys pointing to "our" tables $tableNames = array(...); $foreignKeyHandlingSql = $this->getForeignKeyHandlingSql($schema, $tableNames, 'old_name', 'new_name'); drop FK constraints foreach ($foreignKeyHandlingSql['drop'] as $sql) { $this->addSql($sql); } rename columns now add back FK constraints foreach ($foreignKeyHandlingSql['add'] as $sql) { $this->addSql($sql); }
public static getForeignKeyHandlingSql ( Doctrine\DBAL\Schema\Schema $schema, Doctrine\DBAL\Platforms\AbstractPlatform $platform, array $tableNames, string $search, string $replace ) : array
$schema Doctrine\DBAL\Schema\Schema
$platform Doctrine\DBAL\Platforms\AbstractPlatform
$tableNames array
$search string
$replace string
Результат array
    public static function getForeignKeyHandlingSql(Schema $schema, AbstractPlatform $platform, $tableNames, $search, $replace)
    {
        $foreignKeyHandlingSql = ['drop' => [], 'add' => []];
        $tables = $schema->getTables();
        foreach ($tables as $table) {
            $foreignKeys = $table->getForeignKeys();
            foreach ($foreignKeys as $foreignKey) {
                if (!in_array($table->getName(), $tableNames) && !in_array($foreignKey->getForeignTableName(), $tableNames)) {
                    continue;
                }
                $localColumns = $foreignKey->getLocalColumns();
                $foreignColumns = $foreignKey->getForeignColumns();
                if (in_array($search, $foreignColumns) || in_array($search, $localColumns)) {
                    if (in_array($foreignKey->getLocalTableName(), $tableNames)) {
                        array_walk($localColumns, function (&$value) use($search, $replace) {
                            if ($value === $search) {
                                $value = $replace;
                            }
                        });
                    }
                    if (in_array($foreignKey->getForeignTableName(), $tableNames)) {
                        array_walk($foreignColumns, function (&$value) use($search, $replace) {
                            if ($value === $search) {
                                $value = $replace;
                            }
                        });
                    }
                    $identifierConstructorCallback = function ($columnName) {
                        return new Identifier($columnName);
                    };
                    $localColumns = array_map($identifierConstructorCallback, $localColumns);
                    $foreignColumns = array_map($identifierConstructorCallback, $foreignColumns);
                    $newForeignKey = clone $foreignKey;
                    ObjectAccess::setProperty($newForeignKey, '_localColumnNames', $localColumns, true);
                    ObjectAccess::setProperty($newForeignKey, '_foreignColumnNames', $foreignColumns, true);
                    $foreignKeyHandlingSql['drop'][] = $platform->getDropForeignKeySQL($foreignKey, $table);
                    $foreignKeyHandlingSql['add'][] = $platform->getCreateForeignKeySQL($newForeignKey, $table);
                }
            }
        }
        return $foreignKeyHandlingSql;
    }

Usage Example

Пример #1
0
 /**
  * @param Schema $schema
  * @return void
  */
 public function down(Schema $schema)
 {
     $this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql");
     // collect foreign keys pointing to "our" tables
     $tableNames = array('typo3_party_domain_model_abstractparty', 'typo3_party_domain_model_electronicaddress', 'typo3_party_domain_model_person', 'typo3_party_domain_model_personname');
     $foreignKeyHandlingSql = Service::getForeignKeyHandlingSql($schema, $this->platform, $tableNames, 'persistence_object_identifier', 'flow3_persistence_identifier');
     // drop FK constraints
     foreach ($foreignKeyHandlingSql['drop'] as $sql) {
         $this->addSql($sql);
     }
     // rename identifier fields
     $this->addSql("ALTER TABLE typo3_party_domain_model_abstractparty DROP PRIMARY KEY");
     $this->addSql("ALTER TABLE typo3_party_domain_model_abstractparty CHANGE persistence_object_identifier flow3_persistence_identifier VARCHAR(40) NOT NULL");
     $this->addSql("ALTER TABLE typo3_party_domain_model_abstractparty ADD PRIMARY KEY (flow3_persistence_identifier)");
     $this->addSql("ALTER TABLE typo3_party_domain_model_electronicaddress DROP PRIMARY KEY");
     $this->addSql("ALTER TABLE typo3_party_domain_model_electronicaddress CHANGE persistence_object_identifier flow3_persistence_identifier VARCHAR(40) NOT NULL");
     $this->addSql("ALTER TABLE typo3_party_domain_model_electronicaddress ADD PRIMARY KEY (flow3_persistence_identifier)");
     $this->addSql("ALTER TABLE typo3_party_domain_model_person DROP PRIMARY KEY");
     $this->addSql("ALTER TABLE typo3_party_domain_model_person CHANGE persistence_object_identifier flow3_persistence_identifier VARCHAR(40) NOT NULL");
     $this->addSql("ALTER TABLE typo3_party_domain_model_person ADD PRIMARY KEY (flow3_persistence_identifier)");
     $this->addSql("ALTER TABLE typo3_party_domain_model_personname DROP PRIMARY KEY");
     $this->addSql("ALTER TABLE typo3_party_domain_model_personname CHANGE persistence_object_identifier flow3_persistence_identifier VARCHAR(40) NOT NULL");
     $this->addSql("ALTER TABLE typo3_party_domain_model_personname ADD PRIMARY KEY (flow3_persistence_identifier)");
     // add back FK constraints
     foreach ($foreignKeyHandlingSql['add'] as $sql) {
         $this->addSql($sql);
     }
 }
All Usage Examples Of Neos\Flow\Persistence\Doctrine\Service::getForeignKeyHandlingSql