Skip to content

Commit 7c2b8cf

Browse files
authored
Merge pull request mstaack#101 from mypharmabr/master
Add geometry update support
2 parents bab919c + 6261e1c commit 7c2b8cf

File tree

2 files changed

+35
-30
lines changed

2 files changed

+35
-30
lines changed

src/Eloquent/Builder.php

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,21 @@ public function update(array $values)
99
{
1010
foreach ($values as $key => &$value) {
1111
if ($value instanceof GeometryInterface) {
12-
$value = $this->asWKT($value);
12+
if (is_null($this->model)) {
13+
$value = $this->asWKT($value);
14+
} else {
15+
$attrs = $this->model->getPostgisType($key);
16+
$value = $this->model->asWKT($value, $attrs);
17+
}
1318
}
1419
}
1520

1621
return parent::update($values);
1722
}
1823

19-
protected function getPostgisFields()
20-
{
21-
return $this->getModel()->getPostgisFields();
22-
}
23-
24-
2524
protected function asWKT(GeometryInterface $geometry)
2625
{
27-
return $this->getQuery()->raw(
28-
sprintf("%s.ST_GeogFromText('%s')", function_exists('config') ? config('postgis.schema') : 'public', $geometry->toWKT())
29-
);
30-
26+
return $this->getQuery()->raw(sprintf("%s.ST_GeogFromText('%s')",
27+
function_exists('config') ? config('postgis.schema') : 'public', $geometry->toWKT()));
3128
}
3229
}

src/Eloquent/PostgisTrait.php

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,41 @@ public function newEloquentBuilder($query)
2424
return new Builder($query);
2525
}
2626

27+
protected function geogFromText(GeometryInterface $geometry)
28+
{
29+
return $this->getConnection()->raw(sprintf("%s.ST_GeogFromText('%s')",
30+
function_exists('config') ? config('postgis.schema') : 'public', $geometry->toWKT()));
31+
}
32+
33+
protected function geomFromText(GeometryInterface $geometry, $srid = 4326)
34+
{
35+
return $this->getConnection()->raw(sprintf("%s.ST_GeomFromText('%s', '%d')",
36+
function_exists('config') ? config('postgis.schema') : 'public', $geometry->toWKT(), $srid));
37+
}
38+
39+
public function asWKT(GeometryInterface $geometry, $attrs)
40+
{
41+
switch (strtoupper($attrs['geomtype'])) {
42+
case 'GEOMETRY':
43+
return $this->geomFromText($geometry, $attrs['srid']);
44+
break;
45+
case 'GEOGRAPHY':
46+
default:
47+
return $this->geogFromText($geometry);
48+
break;
49+
}
50+
}
51+
2752
protected function performInsert(EloquentBuilder $query, array $options = [])
2853
{
2954
foreach ($this->attributes as $key => $value) {
3055
if ($value instanceof GeometryInterface) {
3156
$this->geometries[$key] = $value; //Preserve the geometry objects prior to the insert
3257
if (! $value instanceof GeometryCollection) {
3358
$attrs = $this->getPostgisType($key);
34-
switch (strtoupper($attrs['geomtype'])) {
35-
case 'GEOMETRY':
36-
$this->attributes[$key] = $this->getConnection()->raw(
37-
sprintf("%s.ST_GeomFromText('%s', '%d')",
38-
function_exists('config') ? config('postgis.schema') : 'public', $value->toWKT(), $attrs['srid'])
39-
);
40-
break;
41-
case 'GEOGRAPHY':
42-
default:
43-
$this->attributes[$key] = $this->getConnection()->raw(
44-
sprintf("%s.ST_GeogFromText('%s')",
45-
function_exists('config') ? config('postgis.schema') : 'public', $value->toWKT())
46-
);
47-
break;
48-
}
59+
$this->attributes[$key] = $this->asWKT($value, $attrs);
4960
} else {
50-
$this->attributes[$key] = $this->getConnection()->raw(
51-
sprintf("%s.ST_GeomFromText('%s', 4326)",
52-
function_exists('config') ? config('postgis.schema') : 'public', $value->toWKT())
53-
);
61+
$this->attributes[$key] = $this->geomFromText($value);
5462
}
5563
}
5664
}

0 commit comments

Comments
 (0)