Однако в бэкэнде у меня возникли проблемы. Кажется, что Silverstripe CMS выполняет собственное преобразование (например, добавляет разделители тысяч), что дает интересные результаты

Как бы вы, ребята, подошли к этой проблеме? Я пытался использовать onbeforewrite и собственный метод получения.
Это код, который у меня есть, начиная с расширения целочисленного поля базы данных
/**
* Format a number to currency
* @param int $number_of_decimals When larger than 0 it will return this number of decimals, AND divide the amount by 10^number of the amount of decimals
* @param bool $round Round the resulting number to the closest whole number
* @param string $thousands_char Character used as thousands separator
* @param string $decimal_char Character used as decimal separator
* @return string
*/
public function toCurrency($number_of_decimals=2, $round=false, $thousands_char=".", $decimal_char=",") {
$divide_by = pow(10,$number_of_decimals);
$value = $this->owner->value/$divide_by;
if($round) {
//no decimals when rounding

$number_of_decimals=0;
}
return number_format($value, $number_of_decimals, $decimal_char,$thousands_char);
}
public function fromCurrency($number_of_decimals=2, $thousands_char=".", $decimal_char=",") {
$multiply_by = pow(10,$number_of_decimals);
//get rid of the thousand separator
$value = str_replace($thousands_char,"",$this->owner->value);
//replace the decimal char with a point
$value = str_replace($decimal_char,".",$value);
$value = $value*$multiply_by;
return number_format($value, 0, ".","");
}
Также я добавил это в расширение SiteConfig (таким образом создав своего рода глобально доступную функцию
/**
* Creates a DBField equivalent of the value, based on the type. In such a way, we can make use of the dame functions that are in an extension of a dbfield.
* @param $type The type of the DBfield to create (e.g. Varchar, Int etc.).
* @param $value The value, a string or number
* @return mixed
*/
public function ToDBField($type,$value) {
$field = $type::create();
$field->setValue($value);
return $field;
}
Эти функции выполняют фактическую работу и находятся в объекте данных:
public function GetAmount() {
$amount = parent::getField("Amount");
if (is_subclass_of(Controller::curr(), "LeftAndMain")) {
$int_amount = SiteConfig::current_site_config()->ToDBField("Int", $amount);
return $int_amount->toCurrency($number_of_decimals=2, $round=false, $thousands_char="", $decimal_char=".");
}
return $amount;
}
public function onBeforeWrite() {
$int_amount = SiteConfig::current_site_config()->ToDBField("Int", $this->Amount);
$this->Amount = $int_amount->fromCurrency(2,",",".");
parent::onBeforeWrite();
}
Подробнее здесь: https://stackoverflow.com/questions/374 ... -cms-field