cakephp 模型保存浮点值的问题
我有一个名为“地点”的表来保存有关地点的信息
+------------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | place_name | varchar(120) | NO | | NULL | | | latitude | float(10,6) | NO | | NULL | | | longitude | float(10,6) | NO | | NULL | | +------------------+--------------+------+-----+---------+----------------+
现在,当我尝试通过 cakephp 中的 模型 将值插入到表中时,就像
[Place] => Array ( [place_name] => St Francis De Sales [latitude] => 42.381486 [longitude] => -71.066718 )
从 cakephp< 执行的查询 一样/strong> 将值四舍五入为 4 个小数,我不希望这种情况发生。
INSERT INTO `places` (`place_name`, `latitude`, `longitude`) VALUES ('St Francis De Sales', 42.3815, -71.0667)
当我检查表时,纬度和经度值被四舍五入或更改。
+-------------------------------+-----------+------------+ | place_name | latitude | longitude | +-------------------------------+-----------+------------+ | Saint Francis De Sales Church | 42.379501 | -71.062798 | +-------------------------------+-----------+------------+
我直接从 mysql 控制台插入值没有问题。所以,我猜这是一个与 cakephp 有关的问题。我该如何解决这个问题...?
I've a table named 'Places' to save information about places
+------------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | place_name | varchar(120) | NO | | NULL | | | latitude | float(10,6) | NO | | NULL | | | longitude | float(10,6) | NO | | NULL | | +------------------+--------------+------+-----+---------+----------------+
Now when i try to insert values in to the table via a Model in cakephp, say like
[Place] => Array ( [place_name] => St Francis De Sales [latitude] => 42.381486 [longitude] => -71.066718 )
The query being executed from cakephp rounds off the value to 4 fractions and I dont want that to happen..
INSERT INTO `places` (`place_name`, `latitude`, `longitude`) VALUES ('St Francis De Sales', 42.3815, -71.0667)
When i check the table, the latitude and longitude values are rounded off or changed..
+-------------------------------+-----------+------------+ | place_name | latitude | longitude | +-------------------------------+-----------+------------+ | Saint Francis De Sales Church | 42.379501 | -71.062798 | +-------------------------------+-----------+------------+
I have no problem in inserting values directly from mysql console. So, i guess this is an issue related with cakephp. How can i solve this...??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于 CakePHP 使用 sprintf 准备插入查询的方式。
阅读这些错误报告以了解详细信息:
http://cakephp.lighthouseapp.com/projects/42648/tickets/2049-losing-accuracy-when- saving-a-decimal
http://cakephp.lighthouseapp.com/projects/42648 /tickets/2069- precision-loss-when- saving-floats
好消息是这个bug显然已经在1.3.13版本(最新版本)中得到了修复CakePHP 1.3,发布于 2011 年 10 月 15 日)。
因此,您只需升级到 1.3.13 就可以解决您的问题。
如果出于某种原因无法升级,您还可以考虑是否确实有必要将值存储为浮点数。如果您不需要对纬度/经度值进行任何特殊计算,并且不需要按纬度/经度对记录进行排序,则您可以将这些值存储为 varchar。
The problem lies in the way CakePHP uses sprintf to prepare the insert query.
Read through these bug reports for the details:
http://cakephp.lighthouseapp.com/projects/42648/tickets/2049-losing-accuracy-when-saving-a-decimal
http://cakephp.lighthouseapp.com/projects/42648/tickets/2069-precision-loss-when-saving-floats
The good news is that this bug has apparently been fixed in version 1.3.13 (the latest version of CakePHP 1.3, released on 15 October, 2011).
So you should be able to solve your problem by simply upgrading to 1.3.13.
If upgrading isn't an option for whatever reason, you could also consider whether storing the value as a float is really necessary. If you don't have to any special calculations on the lat/long values and you don't need to sort the records by lat/long, you might be able to store the values as varchar instead.