/** * @param \Doctrine\ORM\Tools\Event\SchemaAlterTableChangeColumnEventArgs $args */ public function onSchemaAlterTableChangeColumn(SchemaAlterTableChangeColumnEventArgs $args) { $columnDiff = $args->getColumnDiff(); $column = $columnDiff->column; if (!$column->getType() instanceof \Doctrine\Spatial\DBAL\Types\Type) { return; } if ($columnDiff->hasChanged('spatial_index')) { $diff = $args->getTableDiff(); $tableName = $diff->newName !== false ? $diff->newName : $diff->name; $indexName = $this->generateIndexName($tableName, $column->getName()); if ($column->getCustomSchemaOption('spatial_index')) { $args->addSql(sprintf("CREATE SPATIAL INDEX %s ON %s (%s)", $indexName, $tableName, $column->getQuotedName($platform))); } else { $args->addSql($args->getPlatform()->getDropIndexSQL($indexName, $tableName)); } } // Check if only spatial properties were changed $found = false; foreach ($columnDiff->changedProperties as $property) { if (strpos($property, 'spatial_') === 0) { continue; } $found = true; break; } if (!$found) { $args->preventDefault(); } }
/** * @param \Doctrine\ORM\Tools\Event\SchemaAlterTableChangeColumnEventArgs $args */ public function onSchemaAlterTableChangeColumn(SchemaAlterTableChangeColumnEventArgs $args) { $columnDiff = $args->getColumnDiff(); $column = $columnDiff->column; if (!$column->getType() instanceof \Doctrine\Spatial\DBAL\Types\Type) { return; } $platform = $args->getPlatform(); $diff = $args->getTableDiff(); $tableName = $diff->newName !== false ? $diff->newName : $diff->name; $args->preventDefault(); if ($columnDiff->hasChanged('notnull')) { $query = 'ALTER ' . $column->getQuotedName($platform) . ' ' . ($column->getNotNull() ? 'SET' : 'DROP') . ' NOT NULL'; $args->addSql('ALTER TABLE ' . $tableName . ' ' . $query); } if ($columnDiff->hasChanged('spatial_srid')) { $args->addSql(sprintf("SELECT UpdateGeometrySRID('%s', '%s', %d)", $tableName, $column->getQuotedName($platform), $column->getCustomSchemaOption('spatial_srid'))); } if ($columnDiff->hasChanged('spatial_dimension')) { throw new \RuntimeException('The dimension of a spatial column cannot be changed (Requested changing dimension to "' . $column->getCustomSchemaOption('spatial_dimension') . '" for column "' . $column->getName() . '" in table "' . $diff->name . '")'); } if ($columnDiff->hasChanged('spatial_index')) { $indexName = $this->generateIndexName($tableName, $column->getName()); if ($column->getCustomSchemaOption('spatial_index')) { $args->addSql(sprintf("CREATE INDEX %s ON %s USING GIST (%s)", $indexName, $tableName, $column->getQuotedName($platform))); } else { $args->addSql($platform->getDropIndexSQL($indexName, $tableName)); } } }
public function onSchemaAlterTableChangeColumn(SchemaAlterTableChangeColumnEventArgs $args) { $columnDiff = $args->getColumnDiff(); $column = $columnDiff->column; if (!$this->isSpatialColumnType($column)) { return; } $diff = $args->getTableDiff(); $table = new Identifier(false !== $diff->newName ? $diff->newName : $diff->name); if ($columnDiff->hasChanged('type')) { throw new \RuntimeException('The type of a spatial column cannot be changed (Requested changing type from "' . $columnDiff->fromColumn->getType()->getName() . '" to "' . $column->getType()->getName() . '" for column "' . $column->getName() . '" in table "' . $table->getName() . '")'); } if ($columnDiff->hasChanged('geometry_type')) { throw new \RuntimeException('The geometry_type of a spatial column cannot be changed (Requested changing type from "' . strtoupper($columnDiff->fromColumn->getCustomSchemaOption('geometry_type')) . '" to "' . strtoupper($column->getCustomSchemaOption('geometry_type')) . '" for column "' . $column->getName() . '" in table "' . $table->getName() . '")'); } if ($columnDiff->hasChanged('srid')) { $args->addSql(sprintf("SELECT UpdateGeometrySRID('%s', '%s', %d)", $table->getName(), $column->getName(), $column->getCustomSchemaOption('srid'))); } }