11<?php namespace Phaza \LaravelPostgis \Eloquent ;
22
33use Illuminate \Database \Eloquent \Builder as EloquentBuilder ;
4+ use Illuminate \Support \Arr ;
45use Phaza \LaravelPostgis \Exceptions \PostgisFieldsNotDefinedException ;
56use Phaza \LaravelPostgis \Geometries \Geometry ;
67use Phaza \LaravelPostgis \Geometries \GeometryInterface ;
78
89trait 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