Saving spatial data in CakePHP

2019-05-03 11:08发布

问题:

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()

回答1:

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.



回答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.