Saving spatial data in CakePHP

谁都会走 提交于 2019-12-12 09:40:03

问题


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.



来源:https://stackoverflow.com/questions/16647641/saving-spatial-data-in-cakephp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!