Tharos / LeanMapper

Lean Mapper is a tiny ORM based on powerful Dibi database abstraction library for PHP.
MIT License
87 stars 35 forks source link

Problem with long decimal numbers #98

Closed latisak closed 7 years ago

latisak commented 7 years ago

Hi Tharos,

Im having a strange problem with long decimal numbers. I would like to use SQL number type DOUBLE/DECIMAL.

If I define property using anotation : * @property double $doublevalue

tracy will result with this error

LeanMapper\Exception\InvalidValueException

Property 'doublevalue' in entity App\Model\Entities\Buildings is expected to contain an instance of double, double given.

(I founded that double is not the basicType)

If I define property using method like this

class Buildings extends Entity {

        public function getDoubleVal()
    {
        return (double) $this->row->doublevalue;
    }    

}

This results always with floatvalue.

Is there any correct way how to use leanmapper with long decimal numbers?

Thanks for help.

castamir commented 7 years ago

Have you tried @property float $doublevalue? As far as I know, PHP knows only float which is internally used as C double.

But still, if you still need some special handler for your custom type, try custom getters and setters. They can be set by serveral ways:

/**
 * @property int $id
 * @property string $neonData m:passThru(|neonToString)
 */
class StructuredEntity extends BaseEntity
{
    public function getNeonData()
    {
        $column = $this->row->getMapper()->getColumn(get_class($this), 'neonData');
        $data = Neon::decode($this->row->$column);
        return ArrayHash::from($data);
    }

    public function neonToString($value)
    {
        if (is_string($value)) {
            return $value;
        }
        $adapter = new NeonAdapter();
        return $adapter->dump((array)$value);
    }
}

In the previous example you can see custom getter getNeonData() (setter would be added similar way). And you can see another way, maybe more generic and reusable - neonToString($value) which is a generic "value filter" added via m:passThru(getter|setter) annotation.

Let me know if you find it helpfull or give me detailed use-case what are you actually trying to achieve otherwise.


EDIT: removed redundant data from annotation

latisak commented 7 years ago

Interesting solution.

Unfortunately fail was not in leanmapper or dibi. My database row defined as float was not able to accept long decimal numbers. I changed it to DOUBLE and after parse the result was floatval(654654654654.45), instead of floatval(0).

I dont really realize why there where momentary problem with exception : * @property float $doublevalue

Property 'doublevalue' in entity App\Model\Entities\Buildings is expected to contain an instance of float, double given.

castamir commented 7 years ago

Ok, then, closing