Model properties of DECIMAL data type

Hello,

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.

Thanks!

There are a couple of things you might need to be aware of.

Some countries use up to 3 decimal places.

When converting currencies, you may use up to 4 (or more) decimal places.

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:


<?php echo $form->textField($model,'amount',array('maxlength'=>13,'style'=>'text-align:right')); ?><span class="CUR"></span>

(I set the span content via jQuery depending on other fields)

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 my mysql database the field is decimal len 8,2

But in Model Like this


array('price', 'length', 'max'=><img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' />

sorry my English is poor

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.