为什么propel将float属性转换为string


Why does propel convert float property to string?

我刚刚注意到,在生成的setter方法中将数值转换为字符串。我的问题是,因为我使用德国语言环境,浮点值是用逗号而不是点插入的。例如:"3.5"的结果是字符串"3,5"。我用的是PostgreSQL,显然是3.5版本。

版本信息:如果它是相关的,我使用:PHP 5.3.9, Propel 1.6与Symfony 2.2。

细节:

表定义如下:

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

生成的setAmount()方法如下:

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

保存对象导致PostgreSQL错误:

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

我花了一段时间才找到这个转换发生的地方。正如您所看到的,float值在setAmount()中被转换为字符串。到目前为止,我从未注意到将float值转换为string会导致包含特定于语言环境的小数分隔符的字符串。

我想知道为什么propel首先将浮点值转换为字符串?有什么解决办法吗?

我想到的唯一的解决方法是非常丑陋和烦人的:

setlocale(LC_ALL, 'en_US');
$ae->setAmount(3.5);
setlocale(LC_ALL, 'de_DE');

问题是Propel将与该列相关的PHP字段转换为setter中的本机PHP类型(如@j0k所提到的),但如果深入研究一下,就会发现问题所在。在PropelTypes.php helper类中,在第76行,您可以看到"decimal"的本地PHP类型被列为"string"。

将其与"float"原生类型进行比较,后者被列为"double"。我不确定这是否是有意的,但无论如何,您的问题可以通过简单地将列切换到type="float"type="double"来解决。

Propel应该将其转换为DBMS需要的任何类型。