Skip to content

Commit

Permalink
Fix bug with GeoJSONType.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsegura committed Dec 21, 2017
1 parent 1c23ebb commit 861e85c
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 2 deletions.
28 changes: 28 additions & 0 deletions app/DoctrineMigrations/Version20171220235741.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types = 1);

namespace Application\Migrations;

use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;

/**
* Auto-generated Migration: Please modify to your needs!
*/
class Version20171220235741 extends AbstractMigration
{
public function up(Schema $schema)
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');

$this->addSql('COMMENT ON COLUMN zone.polygon IS \'(DC2Type:geojson)\'');
}

public function down(Schema $schema)
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');

$this->addSql('COMMENT ON COLUMN zone.polygon IS NULL');
}
}
2 changes: 1 addition & 1 deletion app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ doctrine:
commented: false
geojson:
class: 'AppBundle\Doctrine\DBAL\Types\GeoJSONType'
commented: false
commented: true
uuid: Ramsey\Uuid\Doctrine\UuidType
phone_number: Misd\PhoneNumberBundle\Doctrine\DBAL\Types\PhoneNumberType
mapping_types:
Expand Down
2 changes: 1 addition & 1 deletion app/config/doctrine_extensions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ services:
- [ setAnnotationReader, [ "@annotation_reader" ] ]

postgis.event_subscriber:
class: Jsor\Doctrine\PostGIS\Event\DBALSchemaEventSubscriber
class: AppBundle\Doctrine\PostGIS\ORMSchemaEventSubscriber
tags:
- { name: doctrine.event_subscriber, connection: default }

Expand Down
27 changes: 27 additions & 0 deletions src/AppBundle/Doctrine/DBAL/Types/GeoJSONType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@

class GeoJSONType extends GeographyType
{
public function getName()
{
return 'geojson';
}

public function getMappedDatabaseTypes(AbstractPlatform $platform)
{
return array('geojson');
}

public function requiresSQLCommentHint(AbstractPlatform $platform)
{
return true;
}

public function convertToDatabaseValueSQL($sqlExpr, AbstractPlatform $platform)
{
return sprintf('ST_GeographyFromText(ST_AsText(ST_GeomFromGeoJSON(%s)))', $sqlExpr);
Expand All @@ -17,4 +32,16 @@ public function convertToPHPValueSQL($sqlExpr, $platform)
// ::geometry type cast needed for 1.5
return sprintf('ST_AsGeoJSON(%s::geometry)', $sqlExpr);
}

public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
$options = $this->getNormalizedPostGISColumnOptions($fieldDeclaration);

return sprintf(
'%s(%s, %d)',
'geography',
$options['geometry_type'],
$options['srid']
);
}
}
38 changes: 38 additions & 0 deletions src/AppBundle/Doctrine/PostGIS/ORMSchemaEventSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace AppBundle\Doctrine\PostGIS;

use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Event\SchemaAlterTableChangeColumnEventArgs;
use Doctrine\DBAL\Event\SchemaColumnDefinitionEventArgs;
use Jsor\Doctrine\PostGIS\Event\ORMSchemaEventSubscriber as BaseORMSchemaEventSubscriber;

class ORMSchemaEventSubscriber extends BaseORMSchemaEventSubscriber
{
public function onSchemaColumnDefinition(SchemaColumnDefinitionEventArgs $args)
{
parent::onSchemaColumnDefinition($args);

$column = $args->getColumn();

if (!$column) {
return;
}

if (!$this->isSpatialColumnType($column)) {
return;
}

$comment = $args->getColumn()->getComment();

if (!$comment) {
return;
}

$geoJSONComment = $args->getDatabasePlatform()->getDoctrineTypeComment(Type::getType('geojson'));

if ($comment === $geoJSONComment) {
$args->getColumn()->setType(Type::getType('geojson'));
}
}
}

0 comments on commit 861e85c

Please sign in to comment.