xpdo icon indicating copy to clipboard operation
xpdo copied to clipboard

floats are truncated (casted in integer) with andCondition function

Open intersel opened this issue 7 years ago • 3 comments

The issue was initially written in Modx Revolution https://github.com/modxcms/revolution/issues/13657#issuecomment-338891612

Summary

Using floats in a 'andCondition' call would transform them as integer

Step to reproduce

create an XPDO query calling andCondtion as this following exemple

$query->andCondition(
	 array(
		  'location_latitude:>=' => 48.858201356788,
		  'AND:location_latitude:<=' => 48.861798643212,
	 )			
);

Observed behavior

result is like this one :

SELECT `id` FROM `match_request` AS `MatchRequest` 
WHERE ( 
  `MatchRequest`.`location_latitude` >= 48 AND 
  `MatchRequest`.`location_latitude` <= 48
)

Expected behavior

SELECT `id` FROM `match_request` AS `MatchRequest` 
WHERE ( 
  `MatchRequest`.`location_latitude` >= 48.858201356788 AND 
  `MatchRequest`.`location_latitude` <= 48.861798643212
)

Suggestion for solving the problem

Perhaps I missed something but...

The problem was detected in the parseBindings function using the 'quote' function... called line 2670 --2704 in modx revo 2.5.8-- in core/xpdo/xpdo.class.php

if ($type > 0) {
                    $v= $this->quote($v, $type);

Whatever type (int, float, ...) , you call 'quote'...

The problem sounds to be solved by changing line 2534 --2560 in modx revo-- in core/xpdo/xpdo.class.php in function quote

           case PDO::PARAM_INT:
                $quoted = trim($quoted);
                //$quoted = (integer) trim($quoted, "'");//removed
                $quoted = trim($quoted, "'");//added
                break;

Actually, it seems like float are seen like "param_int" (versus param_str) all along the processing... so it should not be cast here as values in float could arrive here...

intersel avatar Oct 24 '17 10:10 intersel

Could you show the relevant portion of your schema? What locale does your server use?

Mark-H avatar Oct 24 '17 11:10 Mark-H

Hi Mark Thanks to take care of my pb...

For the "locale" part, I'm not sure what the server uses... I let everything by default in modx setlocale returns "C"...?

Here my schema:

<?xml version="1.0" encoding="UTF-8"?>
<model package="XXXX" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1">
        <object class="MatchRequest" table="match_request" extends="xPDOSimpleObject">
.......
......
                <field key="location_latitude" dbtype="float" phptype="float" null="false" />
                <field key="location_longitude" dbtype="float" phptype="float" null="false" />
                <field key="location_altitude" dbtype="float" phptype="float" null="false" />
                <field key="location_radiusAccuracy" dbtype="float" phptype="float" null="false" />
......
......
        </object>
</model>

intersel avatar Oct 24 '17 12:10 intersel

Having the exact same issue using xPDO included with MODX 3.01 , where parsing a schema to create Model files:

Deprecated: Implicit conversion from float 2.5 to int loses precision in D:\wamp64\www\modx3\core\vendor\xpdo\xpdo\src\xPDO\Om\xPDOGenerator.php on line 129

Deprecated: Implicit conversion from float 1.5 to int loses precision in D:\wamp64\www\modx3\core\vendor\xpdo\xpdo\src\xPDO\Om\xPDOGenerator.php on line 129

Deprecated: Implicit conversion from float 0.5 to int loses precision in D:\wamp64\www\modx3\core\vendor\xpdo\xpdo\src\xPDO\Om\xPDOGenerator.php on line 129

I get dozens of lines of these errors on two geo-location fields:

        <field key="latitude" dbtype="float" precision="8,6" phptype="float" null="true" />
        <field key="longitude" dbtype="float" precision="9,6" phptype="float" null="true" />

wshawn avatar Sep 27 '22 11:09 wshawn