From a5cfe609d1169cfdbeffd40fa709c40b5a12f745 Mon Sep 17 00:00:00 2001 From: Dylan Lamers <36036362+Dylan-DutchAndBold@users.noreply.github.com> Date: Wed, 5 Dec 2018 10:34:06 +0100 Subject: [PATCH 1/3] Fixes geometry values on created event See issue https://github.com/grimzy/laravel-mysql-spatial/issues/70 --- src/Eloquent/SpatialTrait.php | 38 ++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/Eloquent/SpatialTrait.php b/src/Eloquent/SpatialTrait.php index 5132a622..a7ca6553 100755 --- a/src/Eloquent/SpatialTrait.php +++ b/src/Eloquent/SpatialTrait.php @@ -7,6 +7,7 @@ use Grimzy\LaravelMysqlSpatial\Types\Geometry; use Grimzy\LaravelMysqlSpatial\Types\GeometryInterface; use Illuminate\Database\Eloquent\Builder as EloquentBuilder; +use Illuminate\Database\Eloquent\Model; /** * Trait SpatialTrait. @@ -49,6 +50,25 @@ trait SpatialTrait 'touches', ]; + public static function bootSpatialTrait() + { + static::saving(function (Model $model) { + foreach ($model->attributes as $key => $value) { + if ($value instanceof GeometryInterface) { + $model->geometries[$key] = $value; //Preserve the geometry objects prior to the insert + $model->attributes[$key] = new SpatialExpression($value); + } + } + }); + + $saved = function (Model $model) { + $model->attributes = array_merge($model->attributes, $model->geometries); + }; + + static::saved($saved); + static::created($saved); + } + /** * Create a new Eloquent query builder for the model. * @@ -70,24 +90,6 @@ protected function newBaseQueryBuilder() ); } - 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 - $this->attributes[$key] = new SpatialExpression($value); - } - } - - $insert = parent::performInsert($query, $options); - - foreach ($this->geometries as $key => $value) { - $this->attributes[$key] = $value; //Retrieve the geometry objects so they can be used in the model - } - - return $insert; //Return the result of the parent insert - } - public function setRawAttributes(array $attributes, $sync = false) { $spatial_fields = $this->getSpatialFields(); From a27ed04ff34384380674e780c7dd22541c2e83d7 Mon Sep 17 00:00:00 2001 From: Dylan Lamers <36036362+Dylan-DutchAndBold@users.noreply.github.com> Date: Wed, 5 Dec 2018 10:38:20 +0100 Subject: [PATCH 2/3] Removed unused import/alias --- src/Eloquent/SpatialTrait.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Eloquent/SpatialTrait.php b/src/Eloquent/SpatialTrait.php index a7ca6553..d007dd7b 100755 --- a/src/Eloquent/SpatialTrait.php +++ b/src/Eloquent/SpatialTrait.php @@ -6,7 +6,6 @@ use Grimzy\LaravelMysqlSpatial\Exceptions\UnknownSpatialRelationFunction; use Grimzy\LaravelMysqlSpatial\Types\Geometry; use Grimzy\LaravelMysqlSpatial\Types\GeometryInterface; -use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Eloquent\Model; /** From b5b19b88ba485b314c3049454ae38dca54cbcf06 Mon Sep 17 00:00:00 2001 From: Dylan Lamers <36036362+Dylan-DutchAndBold@users.noreply.github.com> Date: Tue, 11 Dec 2018 15:20:24 +0100 Subject: [PATCH 3/3] Fix where saved event fires too late when observing updated event. Updated event is fired before saved. So to make this work on observers observing the updated event we need to invoke on the updated event instead of the saved event. --- src/Eloquent/SpatialTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Eloquent/SpatialTrait.php b/src/Eloquent/SpatialTrait.php index d007dd7b..0c336d50 100755 --- a/src/Eloquent/SpatialTrait.php +++ b/src/Eloquent/SpatialTrait.php @@ -64,7 +64,7 @@ public static function bootSpatialTrait() $model->attributes = array_merge($model->attributes, $model->geometries); }; - static::saved($saved); + static::updated($saved); static::created($saved); }