laravel-mysql-spatial icon indicating copy to clipboard operation
laravel-mysql-spatial copied to clipboard

Update spatial column failed

Open sid606 opened this issue 3 years ago • 3 comments

When I want to save new object with spatial column the create is good but when I want to update existing model it fail and it give the following error: "SQLSTATE[22003]: Numeric value out of range: 1416 Cannot get geometry object from data you send to the GEOMETRY field (SQL: update locations set point_lat_lng = -23.9878441 40.7484404, locations.updated_at = 2021-04-30 15:58:02 where id = 444).

As you can see this is the problem point_lat_lng = -23.9878441 40.7484404 is not converted into spatial file,

And this is on insert: insert into locations (point_lat_lng, updated_at, created_at) values (ST_GeomFromText(?, ?, 'axis-order=long-lat'), ?, ?) - array (size=4) 0 => string 'POINT(13.4113999 52.5234051)' (length=28) 1 => int 4326 2 => string '2021-04-30 15:36:42' (length=19)

Any help or sugestion?

sid606 avatar Apr 30 '21 16:04 sid606

I got the same issue. Temporary made the worst fix by deleting and inserting the object again.

Mathias-Ooms avatar Jul 21 '21 16:07 Mathias-Ooms

I believe this is because there is no performUpdate function in the SpatialTrait. In my environment I've taken a copy of SpatialTrait and I'm using that instead. I've added the following function just after performInsert and now my updates to points work:

protected function performUpdate(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 update
			$this->attributes[$key] = new SpatialExpression($value);
		}
	}

	$update= parent::performUpdate($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 $update; //Return the result of the parent update
}

moridianmess avatar Dec 14 '21 10:12 moridianmess

I can confirm that adding the performUpdate method does the trick.. can this be turned into a PR?

coogle avatar Sep 16 '22 05:09 coogle