(NOT Yii bug) Floating point database insertion with comma decimal separator locale

This is NOT a Yii bug, I am merely pointing out this behaviour so that others may not lose time over it. Indeed, it seems to me that it is an undesirable behaviour of PHP itself. This will affect DAO and ActiveRecord database actions.

It would seem that the locale set in the PHP environment affects the way that variables are passed around.

So, if you have a floating-point variable, and your locale is set to somewhere that uses a comma as the decimal separator, when you make a PDO insert query using the float value, the value is passed to the database (I haven’t delved into this deeply enough, but I’m assuming that PDO shouldn’t be handling all parameters as text) with the comma decimal separator, and as such the database quite probably will not recognise it and therefore use only the integer value.

There doesn’t seem to be any satisfactory solution to this, so I have ended up following this advice for the moment, and have inserted


setlocale(LC_NUMERIC, 'en_US.utf8');

at the top of my index.php.

It should be noted that the locale name that you will wish to use for this is not necessarily fixed, and will depend on the locales available on the server, so (if you have command line access) you should try something like ‘locale -a’ (for a unix environment) to see what locales are available.

If anyone has any thoughts on how to deal with this better, or some great solution the please do share.