MODX Community

[Solved] Decimal Values Being Trucated on XPDO Object Save

I have an odd problem (again).

I’ve made a custom orders table. In the table I have some decimal fields to hold order total and subtotal.

Here’s an excerpt of the schema for those fields:

 <field key="total" dbtype="decimal" precision="6,2" default="0" phptype="double" null="false"/>
     <field key="subtotal" dbtype="decimal" precision="6,2" default="0" phptype="double" null="false"/>

Here’s the relevant extract from the mysql mapping class:

'total' =>
    array (
      'dbtype' => 'decimal',
      'precision' => '6,2',
      'phptype' => 'double',
      'null' => false,
      'default' => 0,
    ),
    'subtotal' =>
    array (
      'dbtype' => 'decimal',
      'precision' => '6,2',
      'phptype' => 'double',
      'null' => false,
      'default' => 0,
    ),

I’ve written a migration script that pulls in the values in a legacy orders table using a pdo connection and creates new order objects for the new table. This works fine, both the total and subtotal values are entered correctly.

However, if I create an order object in a snippet or processor or indeed in the order class itself, the decimal values are getting truncated - the number after the decimal point is being stripped.

For example I do

$modNewOrder = $modx->newObject('rdOrder');
  $oar = array(
    'user_id' => 52327,
    'vendor_id' => 72846,
    'started' => '2020-03-14 22:15:57',
    'completed' => '2020-03-14 22:15:57',
    'total' => 588.9,
    'subtotal' => 565,
    'handling_charge' => 17.77,
    'transaction_fee_percentage' => 0.04,
    'transaction_id' => '1584224174904216',
    'legacy_id' => 127695
  );
  $modNewOrder->fromArray($oar);
  $modNewOrder->save();

All the decimal values are truncated, so 588 is saved for the total value and 17 for the handling charge.

Anyone got any ideas what’s going on here. I can’t see the difference between running the import script (which runs externally to MODX) and the snippet here - the code is the same.

In the import script I’m initiating MODX as:

define('MODX_CORE_PATH', '/home/user/dev/core/');

require_once MODX_CORE_PATH . 'model/modx/modx.class.php';

$modx= new modX();
$modx->initialize('web');
$modx->runSnippet('migrate-orders');

I’ve seen another post mentioning an issue with locales/date formats which can produce this problem (https://forums.modx.com/thread/85069/xpdo-doesn-t-take-floats) but the actual issue in the old MODX issue tracker no longer exists so I can’t check it.

Though I think this is a long shot, here’s the output of the locale command from the server:

LANG=en_IE.UTF-8
LC_CTYPE="en_IE.UTF-8"
LC_NUMERIC="en_IE.UTF-8"
LC_TIME="en_IE.UTF-8"
LC_COLLATE="en_IE.UTF-8"
LC_MONETARY="en_IE.UTF-8"
LC_MESSAGES="en_IE.UTF-8"
LC_PAPER="en_IE.UTF-8"
LC_NAME="en_IE.UTF-8"
LC_ADDRESS="en_IE.UTF-8"
LC_TELEPHONE="en_IE.UTF-8"
LC_MEASUREMENT="en_IE.UTF-8"
LC_IDENTIFICATION="en_IE.UTF-8"
LC_ALL=

There is no double phptype. That needs to be float.

Ahhhhhhh, now I feel dumb. That’s sorted it.