Yii Framework Forum: Model properties of DECIMAL data type - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Model properties of DECIMAL data type MySQL Rate Topic: -----

#1 User is offline   Vojtech 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 09-April 12
  • Location:Prague

Posted 10 April 2012 - 11:43 AM

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!
0

#2 User is offline   outrage 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 312
  • Joined: 10-November 09
  • Location:Blackpool, United Kingdom

Posted 10 April 2012 - 12:01 PM

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.
0

#3 User is offline   Vojtech 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 09-April 12
  • Location:Prague

Posted 10 April 2012 - 02:30 PM

View Postoutrage, on 10 April 2012 - 12:01 PM, said:

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?
0

#4 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,165
  • Joined: 05-January 12
  • Location:Morocco

Posted 10 April 2012 - 05:24 PM

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)
0

#5 User is offline   Vojtech 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 09-April 12
  • Location:Prague

Posted 11 April 2012 - 02:34 AM

View Postbennouna, on 10 April 2012 - 05:24 PM, said:

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:

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.
0

#6 User is offline   thxyii 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 15-April 13

Posted 27 April 2013 - 03:29 AM

In my mysql database the field is decimal len 8,2

But in Model Like this

array('price', 'length', 'max'=>8)


sorry my English is poor
0

#7 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,455
  • Joined: 04-March 10
  • Location:UK

Posted 27 April 2013 - 03:54 AM

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users