diff --git a/CHANGELOG.md b/CHANGELOG.md index 4bfe6ac..6d8577c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Fixed builder macro docblocks to use FQDN for an issue within laravel-ide-helper - Fixed `ST_PROJECT` function not being migrated to the GeometryType enum. +- Fixed model attribute equality checks for geometries, reduces unnecessary database updates (thanks @adamczykpiotr) ## [1.4.0](https://github.com/clickbar/laravel-magellan/tree/1.4.0) - 2023-07-02 diff --git a/src/Database/Eloquent/HasPostgisColumns.php b/src/Database/Eloquent/HasPostgisColumns.php index 5d98f58..1e72e92 100644 --- a/src/Database/Eloquent/HasPostgisColumns.php +++ b/src/Database/Eloquent/HasPostgisColumns.php @@ -10,10 +10,14 @@ use Clickbar\Magellan\IO\Generator\WKT\WKTGenerator; use Clickbar\Magellan\IO\Parser\WKB\WKBParser; use Illuminate\Database\Eloquent\Builder as EloquentBuilder; +use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Arr; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Config; +/** + * @mixin Model + */ trait HasPostgisColumns { public function getPostgisTypeAndSrid(string $key) @@ -141,6 +145,22 @@ public function setRawAttributes(array $attributes, $sync = false) return parent::setRawAttributes($attributes, $sync); } + public function originalIsEquivalent($key) + { + $this->assertPostgisColumnsNotEmpty(); + + // Not a postgis column + if (array_key_exists($key, $this->postgisColumns) === false) { + return parent::originalIsEquivalent($key); + } + + $attribute = Arr::get($this->attributes, $key); + $original = Arr::get($this->original, $key); + + // Compare already cast objects + return $attribute == $original; + } + protected function assertPostgisColumnsNotEmpty() { if (! property_exists($this, 'postgisColumns')) {