You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
useDecimal\Decimal;
useDoctrine\DBAL\Platforms\AbstractPlatform;
useDoctrine\DBAL\Types\DecimalTypeasDoctrineDecimalType;
class DecimalType extends DoctrineDecimalType
{
publicfunctiongetSQLDeclaration(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' : '');
}
publicfunctionconvertToPHPValue($value, AbstractPlatform$platform)
{
if (is_null($value)) {
returnnull;
}
returnnewDecimal($value, ???); // here precision would be useful
}
publicfunctionconvertToDatabaseValue($value, AbstractPlatform$platform)
{
if ($valueinstanceof Decimal) {
// HERE!! This is the line where I need to specify decimal precision based on column declaration of "scale"return$value->toFixed(????);
}
returnparent::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!
The text was updated successfully, but these errors were encountered:
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.
@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
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
Feature Request
I would like to get column options just like in
getSQLDeclaration()
also inconvertTo[PHP|Database]Value
Summary
I have defined my own
DecimalType
to use native PHP Decimals. The column declaration looks likeThe
DecimalType
is below: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!
The text was updated successfully, but these errors were encountered: