How to use a Point or Geometry type in Datamapper (codeigniter)

纵然是瞬间 提交于 2019-12-24 10:27:34

问题


I'm using codeigniter and datamapper to build an API service (on mysql), and part of it involves geolocation - however I can't seem to find a workaround to use the point datatype. If I try to insert GeomFromText, the system treats it as a string.

Can anyone help?

Cheers, Mark.


回答1:


Try

CREATE TABLE Points ( 
    ID INT(10) PRIMARY KEY NOT NULL AUTO_INCREMENT, 
    location POINT NOT NULL, 
    SPATIAL INDEX(location) 
) ENGINE= MYISAM

In CodeIgniter:

$this->db->set("location",'geomfromtext("POINT(30.2 40.3)")',false);
$this->db->insert("Points");

In CodeIgniter with Datamaper (see help for "Using Formulas in Updates" in the documentation)...

$point = new Point();
$point->update('location','geomfromtext("POINT(30.2 40.3)")',FALSE);



回答2:


As explained in the CodeIgniter User's Guide:

$this->db->set();

set function enables you to set values for inserts or updates.

set() will accepts a third parameter ($escape), that will prevent data from being escaped if set to FALSE.

$this->db->set('geo', "ST_GeomFromText('{$data['geo']}')", FALSE);
$this->db->insert('tableName');

return $this->db->insert_id() ? $this->db->insert_id() : FALSE;



回答3:


Use FLOAT as your database datatype.



来源:https://stackoverflow.com/questions/5265892/how-to-use-a-point-or-geometry-type-in-datamapper-codeigniter

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