Skip to content

Commit

Permalink
Merge 8696d9d into 278a6c1
Browse files Browse the repository at this point in the history
  • Loading branch information
mitulgolakiya committed Sep 16, 2017
2 parents 278a6c1 + 8696d9d commit 8ab9193
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 27 deletions.
13 changes: 1 addition & 12 deletions src/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,10 @@ public function update(array $values)
{
foreach ($values as $key => &$value) {
if ($value instanceof GeometryInterface) {
$value = $this->asWKT($value);
$value = $this->getModel()->getPostgisValue($key, $value);
}
}

return parent::update($values);
}

protected function getPostgisFields()
{
return $this->getModel()->getPostgisFields();
}


protected function asWKT(GeometryInterface $geometry)
{
return $this->getQuery()->raw(sprintf("public.ST_GeogFromText('%s')", $geometry->toWKT()));
}
}
34 changes: 19 additions & 15 deletions src/Eloquent/PostgisTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

trait PostgisTrait
{

public $geometries = [];
/**
* Create a new Eloquent query builder for the model.
Expand All @@ -29,20 +28,7 @@ protected function performInsert(EloquentBuilder $query, array $options = [])
foreach ($this->attributes as $key => $value) {
if ($value instanceof GeometryInterface) {
$this->geometries[$key] = $value; //Preserve the geometry objects prior to the insert
if (! $value instanceof GeometryCollection) {
$attrs = $this->getPostgisType($key);
switch (strtoupper($attrs['geomtype'])) {
case 'GEOMETRY':
$this->attributes[$key] = $this->getConnection()->raw(sprintf("public.ST_GeomFromText('%s', '%d')", $value->toWKT(), $attrs['srid']));
break;
case 'GEOGRAPHY':
default:
$this->attributes[$key] = $this->getConnection()->raw(sprintf("public.ST_GeogFromText('%s')", $value->toWKT()));
break;
}
} else {
$this->attributes[$key] = $this->getConnection()->raw(sprintf("public.ST_GeomFromText('%s', 4326)", $value->toWKT()));
}
$this->attributes[$key] = $this->getPostgisValue($key, $value);
}
}

Expand All @@ -55,6 +41,24 @@ protected function performInsert(EloquentBuilder $query, array $options = [])
return $insert; //Return the result of the parent insert
}

public function getPostgisValue($key, $value)
{
if (! $value instanceof GeometryCollection) {
$attrs = $this->getPostgisType($key);
switch (strtoupper($attrs['geomtype'])) {
case 'GEOMETRY':
return $this->getConnection()->raw(sprintf("public.ST_GeomFromText('%s', '%d')", $value->toWKT(), $attrs['srid']));
break;
case 'GEOGRAPHY':
default:
return $this->getConnection()->raw(sprintf("public.ST_GeogFromText('%s')", $value->toWKT()));
break;
}
}

return $this->getConnection()->raw(sprintf("public.ST_GeomFromText('%s', 4326)", $value->toWKT()));
}

public function setRawAttributes(array $attributes, $sync = false)
{
$pgfields = $this->getPostgisFields();
Expand Down

0 comments on commit 8ab9193

Please sign in to comment.