Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make column information available to convertTo[PHP|Database]Value functions #5341

Open
michnovka opened this issue Apr 2, 2022 · 2 comments

Comments

@michnovka
Copy link

michnovka commented Apr 2, 2022

Feature Request

I would like to get column options just like in getSQLDeclaration() also in convertTo[PHP|Database]Value

Q A
New Feature yes
RFC no

Summary

I have defined my own DecimalType to use native PHP Decimals. The column declaration looks like

#[ORM\Column(type: 'decimal', precision: 10, scale: 4)]
private Decimal $subtotal;

The DecimalType is below:

use Decimal\Decimal;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\DecimalType as DoctrineDecimalType;

class DecimalType extends DoctrineDecimalType
{
    public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
    {
        $column['precision'] = empty($column['precision'])
            ? 10 : $column['precision'];
        $column['scale']     = empty($column['scale'])
            ? 0 : $column['scale'];

        return 'DECIMAL(' . $column['precision'] . ', ' . $column['scale'] . ')' .
            (!empty($column['unsigned']) ? ' UNSIGNED' : '');
    }

    public function convertToPHPValue($value, AbstractPlatform $platform)
    {
        if (is_null($value)) {
            return null;
        }

        return new Decimal($value, ???); // here precision would be useful
    }

    public function convertToDatabaseValue($value, AbstractPlatform $platform)
    {
        if ($value instanceof Decimal) {
            // HERE!! This is the line where I need to specify decimal precision based on column declaration of "scale"
            return $value->toFixed(????);
        }

        return parent::convertToDatabaseValue($value, $platform); // TODO: Change the autogenerated stub
    }
}

I need to format the value based on the scale/precision parameters from the Column definition.

Obviously a dirty hack would be to make several types to suite my needs like decimal-10-6, decimal-10-4,... But you can see why that is just a bad way to go.

I am sure there are many other examples where the column options would be useful to know when doing the casting. One that comes to my mind is unsigned operations.

Here is the ralated StackOverflow question I posted.

Thanks!

@morozov
Copy link
Member

morozov commented Apr 5, 2022

To me, this issue falls under the same "type parametrization" category as we discussed recently in #4930. The DecimalType associated with a column should be parametrized with the values of precision and scale. Each instance of the DecimalType class should handle the convertToPHPValue() calls according to its configuration w/o that configuration being passed upon each call.

Also, linking to #2841 for reference.

@michnovka
Copy link
Author

@morozov I agree. These issues keep piling up in my project, parametrization would help with enumTypes as discussed in your referenced issue and would also be a solution to this: doctrine/orm#9622

@morozov morozov changed the title Pass column information to convertTo[PHP|Database]Value functions Make column information available to convertTo[PHP|Database]Value functions May 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants