Saving spatial data in CakePHP
Asked Answered
D

2

6

I have a problem saving spatial data in CakePHP with saveAll(). I really dont want to write the query manually (Handling spatial data in CakePHP) because there are number of models being saved.

Also I read this CakePHP and MySQL Spatial Extension but when I try to do the same, $db->expression() returns an stdClass.

This is the returned object printed out:

stdClass Object
(
    [type] => expression
    [value] => GeomFromText('POINT(48.18879 18.527579999999944)')
)

If I use this object the way it is used in CakePHP and MySQL Spatial Extension and try to save it with saveAll() I get this error:

Error: Cannot use object of type stdClass as array
File: /www/s/t/u47728/public_html/lib/Cake/Model/Model.php
Line: 2221

If I use the value property, it gets escaped in the query so it becomes just a string. Then I get this error:

Error: SQLSTATE[22003]: Numeric value out of range: 1416 Cannot get geometry object from data you send to the GEOMETRY field

Does saveAll() suport expressions?

UPDATE

Apparently the same applies to save() function and others.... Also saveField()

Dentil answered 20/5, 2013 at 10:35 Comment(0)
W
6

convert this line :

$this->data['Report']['position'] = $db->expression("GeomFromText('POINT(" . 
    $this->data['Report']['lat'] . " " . $this->data['Report']['lng'] . ")')");

to :

$this->data['Report']['position'] = (object) $db->expression("GeomFromText('POINT(" .
     $this->data['Report']['lat'] . " " . $this->data['Report']['lng'] . ")')");

It should work.

Wrestle answered 20/5, 2013 at 13:36 Comment(1)
I tried the code you posted and it didn't work, however typecasting an object was the key so I tried $this->data['Report']['position'] = (object) $db->expression("GeomFromText('POINT(" . $this->data['Report']['lat'] . " " . $this->data['Report']['lng'] . ")')"); and it worked. So marking your answer as accepted anyways because it lead me to the solution. Thank you very muchDentil
V
2

You should also change the order of your data.

The right way is: POINT(longitude latitude)

Couldn't comment on the right answer as i haven't enough reputation.

Varitype answered 26/11, 2014 at 11:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.