如何防止CakePHP在保存时转义数据? [英] How can I prevent CakePHP from escaping data on a save?

查看:128
本文介绍了如何防止CakePHP在保存时转义数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在CakePHP应用程序上做一些PostGIS工作。



因为我一直在使用一些数据库函数,我已经做了 $ this-> query()调用以执行数据插入。我在一个点,我需要得到一个插入查询的结果的ID,但 $ this-> query()返回一个空数组。 p>

这是我使用的插入查询:

  INSERT INTO位置(title,company_id,state_id,poly,point)
VALUES('$ title',$ company_id,$ state_id,ST_GeomFromText('$ geom',4269),$ point);

问题是Cake尝试插入'ST_GeomFromText('$ geom' ,4269)'作为一个字符串,我不知道如何获取它删除SQL插入语句它通过 $ this-> Location-> ; save();



有任何建议吗?





之前我做过 $ this-> query(); 精细。这不是问题。



现在当我这样做:

  $ this-> Location-> create(); 
$ this-> Location-> set('poly',$ poly);

...设置所有其余的模型字段...

$ this-> Location-> save();

生成的SQL如下所示:

  INSERT INTO locations(title,company_id,state_id,poly,point)
VALUES('Some Title',5,23,'ST_GeomFromText('$ geom',4269 )',POINT(-72.342,102.23455);

因为ST_GeomFromText得到'quoted',PostgreSQL抛出错误,并且不插入几何。

解决方案

我的问题的解决方案不是基于CakePHP的解决方案,一个PostgreSQL一个!



通过在查询中添加RETURNING id,如下所示:

 code> INSERT INTO locations(title,company_id,state_id,poly,point)
VALUES('$ title',$ company_id,$ state_id,ST_GeomFromText('$ geom',4269),$ point)RETURNING id;

查询不再返回一个空数组,现在返回它的行的ID创建了!


I am doing some PostGIS work on a CakePHP application.

Because I've been working with some database functions, I've done raw $this->query() calls to do inserts of data. I'm at a point where I need to get the ID of the result of an insert query, but $this->query() returns an empty array.

Here is the query I'm using for inserts:

INSERT INTO locations (title,company_id,state_id,poly,point) 
VALUES ('$title',$company_id,$state_id,ST_GeomFromText('$geom',4269),$point);

The problem is Cake is trying to insert 'ST_GeomFromText('$geom',4269)' as a string, and I cannot figure out how to get it to remove the quotes in the SQL insert statement it prepares via $this->Location->save();.

Any suggestions?

Edit:

Before I was doing $this->query(); and my SQL works fine. That's not the issue.

Now when I do:

$this->Location->create();
$this->Location->set('poly',$poly);

... Set all the rest of the model fields ...

$this->Location->save();

The SQL that gets produced looks like this:

INSERT INTO locations (title,company_id,state_id,poly,point) 
VALUES ('Some Title',5,23,'ST_GeomFromText('$geom',4269)',POINT(-72.342,102.23455);

Because ST_GeomFromText gets 'quoted', PostgreSQL throws an error and doesn't insert the geometry.

解决方案

The solution to my problem turned out to NOT be a CakePHP based one, but a PostgreSQL one!

By appending "RETURNING id" to the query like this:

INSERT INTO locations (title,company_id,state_id,poly,point) 
VALUES ('$title',$company_id,$state_id,ST_GeomFromText('$geom',4269),$point) RETURNING id;

The query no longer returns an empty array, and now returns the ID of the row it just created!

这篇关于如何防止CakePHP在保存时转义数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆