DB2 datatypes and laminas/laminas-hydrator - Decimal, Numeric and BigInt are treated as strings

I’m running PHP 7.3.11 with laminas/laminas-hydrator 3.0.2 against a DB2 for IBM i database. I’m using the ClassMethodsHydrator with a model class which uses typehinting (on parameters and return type) in the get/set methods.

I’m noticing that DB2 Decimal, Numeric and BigInt datatypes are being treated as strings while Integer DB2 datatypes are correctly treated as a PHP integer.

Is there a way to have Decimal, Numeric and BigInt datatypes treated as PHP integers?

1 Like

I’m not familiar enough with DB2, but with MySQL and DECIMAL types the answer is no.

The introduction of strict_types in 3.0 makes this stuff needlessly difficult IMO - I’m stuck on 2.4.2 until I can work out a way forward for hydrating from DB results.

FYI, an interim solution I’ve come up with is this (in my model class):

/**
 * Get getField1
 *
 * - For Numeric and BigInt datatypes
 *
 * @return int
 */
public function getField1(): int
{
    return (int)$this->Field1;
}

/**
 * Set setField1
 *
 * - For Numeric and BigInt datatypes
 *
 * @param int $Field1
 *
 * @return void
 */
public function setField1($Field1): void
{
    $this->Field1 = (int)$Field1;
}

/**
 * Get getField2
 *
 * - For Decimal datatypes
 *
 * @return float
 */
public function getField2(): float
{
    return (float)$this->Field2;
}

/**
 * Set setField2
 *
 * - For Decimal datatypes
 *
 * @param float $Field2
 *
 * @return void
 */
public function setField2($Field2): void
{
    $this->Field2 = (float)$Field2;
}

Removing typehinting from the parameter in the “set” methods allows for the field to come in as anything (IE: String), but casts it to the correct datatype. If it’s already the correct datatype, then no harm done. Then the return value in the “get” methods also ensure it returns the correct datatype (a little redundant though, I know).

This is just an alternative to using 2.4.2. Perhaps someone’s dependencies require 3.x?