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

Community
  • 1
  • 1

2 Answers2

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.

Arash Mousavi
  • 2,110
  • 4
  • 25
  • 47
  • 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 much –  May 20 '13 at 13:58
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.

philippzentner
  • 396
  • 2
  • 11