I’ve used Gii to create a model from MySQL table which contains some fields of the DECIMAL (10,2) data type (for storing currency amounts).
Gii recognized these fields as a string fields. As I understand the alteration of rules() will be necessary (a change to ‘numerical’, ‘integerOnly’=>false, etc.). I found some posts regarding that.
Yet I’d like to ask you if there are any other consequences that I should be careful about when using a DECIMAL type.
Thank you for your suggestions. It’s actually going to be play money yet I’ll consider adding more decimal places anyway. As you mentioned it may be important for some conversions later on.
Still I’m curious if there any specific issues with DECIMAL type in Yii framework or the only issue is that Gii doesn’t recognize the type and everything else work smoothly?
I’m not sure to understand. I use DECIMAL(10,2) as well (e.g. for amount field), and I think Gii (or is it me?) specified in my model’s validation rules:
array('amount', 'numerical'),
I think the only thing I specified is: the max length, to right-align the text fields in my _form, and add the currency and the VAT inc./excl. mention:
To be sure I’ve tried Gii once more on a different table. It creates:
In description:
// @property string $amount
In rules():
array('amount', 'length', 'max'=>10),
As I mentioned it’s ok for me to change the rules. I just wanted to make sure there are no other consequences that wouldn’t be apparent at a first sight. I’m sorry if I’m bringing up an irrelevant matter. I’ve just started with Yii so I haven’t fully got my feet under the table yet.
In PHP in general, if you’re performing calculations with decimal values, use the bcmath library. Using normal mathematical operators will likely result in floating point imprecision.