This is a behavior for [CActiveRecord]s which converts DECIMALs between DB and locale specific display format.
Requirements ¶
Should work with any 1.1.x version. Not tested with 1.0.x.
Usage ¶
When this behavior is attached to a model, all DECIMAL columns will be formatted with the Yii::app()->numberFormatter component. By default, the default decimal format for the current locale will be used ([CNumberFormatter::formatDecimal]).
Configuration ¶
Like all behaviors this extension has to be configured in the behaviors() method of a model:
public function behaviors()
{
return array(
'defaults'=>array(
'class'=>'ext.decimali18nbehavior.DecimalI18NBehavior',
'format'=>'db',
),
);
}
The complete list of configuration options is:
format- If not set, the default decimal format is used. If set to a format string, this string is supplied for [CNumberFormat::format]. If set todbthe number of decimals will be read from DB. So aDECIMAL(10,4)will be displayed with 4 decimals.formats- Can be an Array of additional name/format pairs, to supply formats per attribute. If not set,formatwill be used instead.parseExpression- If set, this expression will be used to convert a formatted number back to DB format.$valuewill contain the formatted attribute value. For example to convert1,234into1.234the expression can be set tostrtr($value,",",".")
Example ¶
Here's another configuration example for this behavior:
public function behaviors()
{
return array(
'defaults'=>array(
'class'=>'ext.decimali18nbehavior.DecimalI18NBehavior',
'format'=>'db', // use DB format by default
'formats'=> array(
'someCol'=>'#0.0', // specific format for someCol
),
'parseExpression'=> "strtr(\$value, ',' , '.' )",
),
);
}
Resources ¶
Changelog ¶
- 1.0.0 - initial release
Very nice
I wrote formaters for decimal numbers and date in my SuperModel.
But i like that way much more !
parseExpresion
I have problems with parseExpresion does not work, always placing a "," Put the symbol that puts you help ... I apologize for my English ...
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.