brick / math

Arbitrary-precision arithmetic library for PHP
MIT License
1.78k stars 75 forks source link

Integration with Doctrine #34

Closed doMynation closed 4 years ago

doMynation commented 4 years ago

Hi,

First, thank you for all the effort spent on this library. There are many libraries for arbitrary precision maths in PHP, and this one is by far the best I've come across.

I'm not sure if this is the best place to ask this question, so my apologies if it isn't.

How might I integrate this library with Doctrine? I'm mostly only interested in the BigDecimal type. I've tried creating an Embeddable or a custom type, however I'm struggling to define the appropriate scale. In Doctrine, one can define a mapping to a MySQL decimal type as such:

    /**
     * @Column(type="decimal", name="total", precision=19, scale=4)
     */
    protected $total;

I'm trying to map the above field to a BigDecimal($value, 4). Basically, I'm able to define a custom type, though unless I hardcode the scale (or use BigDecimal::of(), as is done here) to a specific number, I'm unable to dynamically set the scale on a per-case basis (e.g. one field with a scale of 4, and another with a different scale).

Thank you!

BenMorel commented 4 years ago

Hi 👋, thank you!

The mapping you've linked to: https://github.com/brick/brick/blob/master/src/Doctrine/Types/Math/BigDecimalType.php

is part of another project of mine, which is unreleased to date, but is the correct way to map a BigDecimal to a MySQL DECIMAL. When retrieved from the database, it will be passed as a string to of(), which will preserve the number of digits after the decimal place.

Let me know if this clears it up for you!


Code is like humor. When you have to explain it, it’s bad.

I love this :)

doMynation commented 4 years ago

Of course! 🤦‍♂️

I somehow thought of() would just default to a scale of 0. Sorry about that, and thank you very much for the prompt response.

I'm closing the issue.