Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit 34b1e75

Browse files
committed
Merge pull request #24 from rogue780/dev
Allow for unspecified geometry in postgisFields
2 parents 4800948 + 7ba65a4 commit 34b1e75

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/Eloquent/PostgisTrait.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
<?php namespace Phaza\LaravelPostgis\Eloquent;
22

33
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
4+
use Illuminate\Support\Arr;
45
use Phaza\LaravelPostgis\Exceptions\PostgisFieldsNotDefinedException;
56
use Phaza\LaravelPostgis\Geometries\Geometry;
67
use Phaza\LaravelPostgis\Geometries\GeometryInterface;
78

89
trait PostgisTrait
910
{
11+
12+
public $geometries = [];
1013
/**
1114
* Create a new Eloquent query builder for the model.
1215
*
@@ -22,16 +25,23 @@ protected function performInsert(EloquentBuilder $query, array $options = [])
2225
{
2326
foreach ($this->attributes as $key => &$value) {
2427
if ($value instanceof GeometryInterface) {
28+
$this->geometries[$key] = $value; //Preserve the geometry objects prior to the insert
2529
$value = $this->getConnection()->raw(sprintf("ST_GeogFromText('%s')", $value->toWKT()));
2630
}
2731
}
2832

29-
return parent::performInsert($query, $options);
33+
$insert = parent::performInsert($query, $options);
34+
35+
foreach($this->geometries as $key => $value){
36+
$this->attributes[$key] = $value; //Retrieve the geometry objects so they can be used in the model
37+
}
38+
39+
return $insert; //Return the result of the parent insert
3040
}
3141

3242
public function setRawAttributes(array $attributes, $sync = false)
3343
{
34-
$pgfields = array_keys($this->getPostgisFields());
44+
$pgfields = $this->getPostgisFields();
3545

3646
foreach ($attributes as $attribute => &$value) {
3747
if (in_array($attribute, $pgfields) && is_string($value) && strlen($value) >= 15) {
@@ -45,7 +55,9 @@ public function setRawAttributes(array $attributes, $sync = false)
4555
public function getPostgisFields()
4656
{
4757
if (property_exists($this, 'postgisFields')) {
48-
return $this->postgisFields;
58+
return Arr::isAssoc($this->postgisFields) ? //Is the array associative?
59+
array_keys($this->postgisFields) : //Returns just the keys to preserve compatibility with previous versions
60+
$this->postgisFields; //Returns the non-associative array that doesn't define the geometry type.
4961
} else {
5062
throw new PostgisFieldsNotDefinedException(__CLASS__ . ' has to define $postgisFields');
5163
}

0 commit comments

Comments
 (0)