Skip to content

Commit

Permalink
Fix #87
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainMazB authored May 17, 2024
1 parent 8cbe80a commit 94bcb35
Showing 1 changed file with 9 additions and 27 deletions.
36 changes: 9 additions & 27 deletions src/Database/Eloquent/HasPostgisColumns.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,49 +84,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 94bcb35

Please sign in to comment.