Extend "Nice" Method of DBDecimal

Version: 4.4

I want to extend or overwrite the “Nice” Method of DBDecimal. I tried to add and register a extension:

MyDBDecimalExtension

But this don’t worked.

Why I need this? I use this field casting in ModelAdmin:

$dataColumns->setFieldCasting([
    'Quantity' => 'Decimal->Nice' 
]);

This returns the value with 2 commas, but I need no commas.

If I use ‘Int->Nice’ I have no thousands seperator.

If I cast it myself, for example with a getFormattedQuantity field, I loose sorting functionality in ModelAdmin.

How to solve this?

HI @d4n333, if you’re using an Extension or DataExtension you can add new properties and methods, and hook in to some methods, but you can’t override existing ones. If you want to override a method you need to sub-class instead.

Instead of overriding Nice, how about adding a DecimalNice method and do:

$dataColumns->setFieldCasting([
    'Quantity' => 'Decimal->DecimalNice' 
]);
<?php

use SilverStripe\ORM\DataExtension;

class MyDBDecimalExtension extends DataExtension 
{

    public function DecimalNice() 
    {
        return number_format($this->owner->value, 0);
    }
}

That worked now, thx!`