Skip to content

Commit 0972a1c

Browse files
committed
Prevent geometry type fields from throwing a 'type not found' error (happens with doctrine/dbal when changing field definitions)
1 parent 65fff72 commit 0972a1c

File tree

12 files changed

+285
-7
lines changed

12 files changed

+285
-7
lines changed

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,59 @@ Note about spatial indexes from the [MySQL documentation](https://dev.mysql.com/
154154

155155
> For [`MyISAM`](https://dev.mysql.com/doc/refman/5.7/en/myisam-storage-engine.html) and (as of MySQL 5.7.5) `InnoDB` tables, MySQL can create spatial indexes using syntax similar to that for creating regular indexes, but using the `SPATIAL` keyword. Columns in spatial indexes must be declared `NOT NULL`.
156156
157+
From the command line:
158+
159+
```shell
160+
php artisan make:migration update_places_table
161+
```
162+
163+
Then edit the migration you just created:
164+
165+
```php
166+
use Illuminate\Database\Migrations\Migration;
167+
use Illuminate\Database\Schema\Blueprint;
168+
use Illuminate\Support\Facades\Schema;
169+
170+
class UpdatePlacesTable extends Migration
171+
{
172+
/**
173+
* Run the migrations.
174+
*
175+
* @return void
176+
*/
177+
public function up()
178+
{
179+
// MySQL < 5.7.5: table has to be MyISAM
180+
// \DB::statement('ALTER TABLE places ENGINE = MyISAM');
181+
182+
Schema::table('places', function (Blueprint $table) {
183+
// Make sure point is not nullable
184+
$table->point('location')->change();
185+
186+
// Add a spatial index on the location field
187+
$table->spatialIndex('location');
188+
});
189+
}
190+
191+
/**
192+
* Reverse the migrations.
193+
*
194+
* @return void
195+
*/
196+
public function down()
197+
{
198+
Schema::table('places', function (Blueprint $table) {
199+
$table->dropSpatialIndex(['location']); // either an array of column names or the index name
200+
});
201+
202+
// \DB::statement('ALTER TABLE places ENGINE = InnoDB');
203+
204+
Schema::table('places', function (Blueprint $table) {
205+
$table->point('location')->nullable()->change();
206+
});
207+
}
208+
}
209+
```
157210
## Models
158211

159212
Available geometry classes:

src/Doctrine/Geometry.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Grimzy\LaravelMysqlSpatial\Doctrine;
4+
5+
use Doctrine\DBAL\Platforms\AbstractPlatform;
6+
use Doctrine\DBAL\Types\Type;
7+
8+
class Geometry extends Type
9+
{
10+
const GEOMETRY = 'geometry';
11+
12+
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
13+
{
14+
return 'geometry';
15+
}
16+
17+
public function getName()
18+
{
19+
return self::GEOMETRY;
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Grimzy\LaravelMysqlSpatial\Doctrine;
4+
5+
use Doctrine\DBAL\Platforms\AbstractPlatform;
6+
use Doctrine\DBAL\Types\Type;
7+
8+
class GeometryCollection extends Type
9+
{
10+
const GEOMETRYCOLLECTION = 'geometrycollection';
11+
12+
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
13+
{
14+
return 'geometrycollection';
15+
}
16+
17+
public function getName()
18+
{
19+
return self::GEOMETRYCOLLECTION;
20+
}
21+
}

src/Doctrine/LineString.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Grimzy\LaravelMysqlSpatial\Doctrine;
4+
5+
use Doctrine\DBAL\Platforms\AbstractPlatform;
6+
use Doctrine\DBAL\Types\Type;
7+
8+
class LineString extends Type
9+
{
10+
const LINESTRING = 'linestring';
11+
12+
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
13+
{
14+
return 'linestring';
15+
}
16+
17+
public function getName()
18+
{
19+
return self::LINESTRING;
20+
}
21+
}

src/Doctrine/MultiLineString.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Grimzy\LaravelMysqlSpatial\Doctrine;
4+
5+
use Doctrine\DBAL\Platforms\AbstractPlatform;
6+
use Doctrine\DBAL\Types\Type;
7+
8+
class MultiLineString extends Type
9+
{
10+
const MULTILINESTRING = 'multilinestring';
11+
12+
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
13+
{
14+
return 'multilinestring';
15+
}
16+
17+
public function getName()
18+
{
19+
return self::MULTILINESTRING;
20+
}
21+
}

src/Doctrine/MultiPoint.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Grimzy\LaravelMysqlSpatial\Doctrine;
4+
5+
use Doctrine\DBAL\Platforms\AbstractPlatform;
6+
use Doctrine\DBAL\Types\Type;
7+
8+
class MultiPoint extends Type
9+
{
10+
const MULTIPOINT = 'multipoint';
11+
12+
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
13+
{
14+
return 'multipoint';
15+
}
16+
17+
public function getName()
18+
{
19+
return self::MULTIPOINT;
20+
}
21+
}

src/Doctrine/MultiPolygon.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Grimzy\LaravelMysqlSpatial\Doctrine;
4+
5+
use Doctrine\DBAL\Platforms\AbstractPlatform;
6+
use Doctrine\DBAL\Types\Type;
7+
8+
class MultiPolygon extends Type
9+
{
10+
const MULTIPOLYGON = 'multipolygon';
11+
12+
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
13+
{
14+
return 'multipolygon';
15+
}
16+
17+
public function getName()
18+
{
19+
return self::MULTIPOLYGON;
20+
}
21+
}

src/Doctrine/Point.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Grimzy\LaravelMysqlSpatial\Doctrine;
4+
5+
use Doctrine\DBAL\Platforms\AbstractPlatform;
6+
use Doctrine\DBAL\Types\Type;
7+
8+
class Point extends Type
9+
{
10+
const POINT = 'point';
11+
12+
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
13+
{
14+
return 'point';
15+
}
16+
17+
public function getName()
18+
{
19+
return self::POINT;
20+
}
21+
}

src/Doctrine/Polygon.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Grimzy\LaravelMysqlSpatial\Doctrine;
4+
5+
use Doctrine\DBAL\Platforms\AbstractPlatform;
6+
use Doctrine\DBAL\Types\Type;
7+
8+
class Polygon extends Type
9+
{
10+
const POLYGON = 'polygon';
11+
12+
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
13+
{
14+
return 'polygon';
15+
}
16+
17+
public function getName()
18+
{
19+
return self::POLYGON;
20+
}
21+
}

src/MysqlConnection.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,28 @@
44

55
class MysqlConnection extends \Illuminate\Database\MySqlConnection
66
{
7-
// public function __construct($pdo, $database = '', $tablePrefix = '', array $config = [])
8-
// {
9-
// parent::__construct($pdo, $database, $tablePrefix, $config);
10-
//
11-
// // Prevent geography type fields from throwing a 'type not found' error.
12-
// $this->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('geography', 'string');
13-
// }
7+
public function __construct($pdo, $database = '', $tablePrefix = '', array $config = [])
8+
{
9+
parent::__construct($pdo, $database, $tablePrefix, $config);
10+
11+
if(class_exists('Doctrine\DBAL\Types\Type')) {
12+
// Prevent geometry type fields from throwing a 'type not found' error when changing them
13+
$geometries = [
14+
'geometry',
15+
'point',
16+
'linestring',
17+
'polygon',
18+
'multipoint',
19+
'multilinestring',
20+
'multipolygon',
21+
'geometrycollection',
22+
];
23+
$dbPlatform = $this->getDoctrineSchemaManager()->getDatabasePlatform();
24+
foreach($geometries as $type) {
25+
$dbPlatform->registerDoctrineTypeMapping($type, 'string');
26+
}
27+
}
28+
}
1429

1530
/**
1631
* Get the default schema grammar instance.

0 commit comments

Comments
 (0)