Skip to content

Overrides getAttributesForInsert & getDirtyForUpdate instead of performInsert & performUpdate to #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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