Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does propel convert float property to string?

Tags:

php

propel

I just noticed, that propel converts numeric values to strings in generated setter methods. My problem with that is, since I am using german locale, float values are insert with a comma instead of a dot. For example: "3.5" results in a string "3,5". I am using PostgreSQL, which obviously expects 3.5.

Version info: In case it is relevant, I am using: PHP 5.3.9, Propel 1.6 with Symfony 2.2.

Details:

The table definition looks like this:

<table name="account_entry">
    ...
    <column name="amount" type="decimal" size="12" scale="2" required="true" />
    ...
</table>

The generated setAmount() method looks like this:

public function setAmount($v)
{
    if ($v !== null && is_numeric($v)) {
        $v = (string) $v;
    }

    if ($this->amount !== $v) {
        $this->amount = $v;
        $this->modifiedColumns[] = AccountEntryPeer::AMOUNT;
    }


    return $this;
} // setAmount()

Saving the object results in a PostgreSQL error:

Invalid text representation: 7 ERROR: invalid input syntax for type numeric: "3,5"

It took me a while to find the place where this conversion happens. As you can see the float value is being casted to string in setAmount(). Up to now I have never noticed that casting a float value to string results in a string containing a locale specific decimal separator.

I wonder why propel converts a float value into a string in the first place? Is there some workaround for this?

The only workaround I came up with is really ugly and annoying:

setlocale(LC_ALL, 'en_US');
$ae->setAmount(3.5);
setlocale(LC_ALL, 'de_DE');
like image 689
Leif Avatar asked Mar 26 '13 08:03

Leif


People also ask

How to convert a string into float?

We can convert a string to float in Python using the float() function. This is a built-in function used to convert an object to a floating point number. Internally, the float() function calls specified object __float__() function.

Can you convert a string to a float in Java?

Strings can be converted to floating point numbers using different methods in Java. The different methods used to convert strings to float are: the valueOf() method, the parseFloat() method, Float class constructor, and the DecimalFormat class.

Can we convert float to string in Python?

In Python, we can use str() to convert float to String.


1 Answers

The problem is that Propel converts the PHP field that relates to that column into a native PHP type in the setter (as @j0k mentions), but if you look a little deeper you see the issue. In the PropelTypes.php helper class, on line 76 you can see that the native PHP type for "decimal" is listed as "string".

Compare this to the "float" native type which is listed as "double". I'm not sure if this is intentional or not, but in any case, your issue may be solved by simply switching the column to type="float" or type="double".

Propel should convert that into whatever type your DBMS requires.

like image 131
Jordan Kasper Avatar answered Sep 24 '22 16:09

Jordan Kasper