Skip to content

Commit

Permalink
Merge pull request #88 from RomainMazB/patch-2
Browse files Browse the repository at this point in the history
Overrides getAttributesForInsert & getDirtyForUpdate instead of performInsert & performUpdate to
  • Loading branch information
saibotk authored Jul 21, 2024
2 parents 750645e + 491b0c9 commit d63bd05
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 28 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Improved
- Only override attribute preparation functions on models instead of `performInsert` or `performUpdate` entirely (thanks @RomainMazB #89)

### Fixed
- Fixed geometries not being passed to model events (fixes #87) (thanks @RomainMazB #89)

## [1.6.0](https://github.com/clickbar/laravel-magellan/tree/1.6.0) - 2024-03-17

### Added
Expand Down
37 changes: 9 additions & 28 deletions src/Database/Eloquent/HasPostgisColumns.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Clickbar\Magellan\IO\Generator\BaseGenerator;
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;
Expand Down Expand Up @@ -84,49 +83,31 @@ public function getGeometryAsInsertable(Geometry $geometry, array $columnConfig)
};
}

protected function transformGeometriesInAttributes(): array
protected function transformGeometryAttributesToExpression(array $attributes): array
{
$geometryCache = [];

foreach ($this->attributes as $key => $value) {
foreach ($attributes as $key => $value) {
if ($value instanceof Geometry) {
$geometryCache[$key] = $value; //Preserve the geometry objects prior to the insert
if ($value instanceof GeometryCollection) {
// --> Only insertable into geometry column types
$this->attributes[$key] = $this->geomFromText($value);
$attributes[$key] = $this->geomFromText($value);
} else {
$columnConfig = $this->getPostgisTypeAndSrid($key);
$this->attributes[$key] = $this->getGeometryAsInsertable($value, $columnConfig);
$attributes[$key] = $this->getGeometryAsInsertable($value, $columnConfig);
}
}
}

return $geometryCache;
return $attributes;
}

protected function restoreOriginalGeometriesInAttributes(array $originalGeometries)
protected function getDirtyForUpdate(): array
{
foreach ($originalGeometries as $key => $value) {
$this->attributes[$key] = $value; //Retrieve the geometry objects, so they can be used in the model
}
return $this->transformGeometryAttributesToExpression(parent::getDirtyForUpdate());
}

protected function performInsert(EloquentBuilder $query, array $options = [])
protected function getAttributesForInsert(): array
{
$originalGeometries = $this->transformGeometriesInAttributes();
$insert = parent::performInsert($query, $options);
$this->restoreOriginalGeometriesInAttributes($originalGeometries);

return $insert; //Return the result of the parent insert
}

protected function performUpdate(EloquentBuilder $query)
{
$originalGeometries = $this->transformGeometriesInAttributes();
$update = parent::performUpdate($query);
$this->restoreOriginalGeometriesInAttributes($originalGeometries);

return $update; //Return the result of the parent insert
return $this->transformGeometryAttributesToExpression(parent::getAttributesForInsert());
}

public function setRawAttributes(array $attributes, $sync = false)
Expand Down

0 comments on commit d63bd05

Please sign in to comment.