为什么推动将float属性转换为字符串?

Lei*_*eif 5 php propel

我只是注意到,推进将数值转换为生成的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>
Run Code Online (Sandbox Code Playgroud)

生成的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()
Run Code Online (Sandbox Code Playgroud)

保存对象会导致PostgreSQL错误:

Invalid text representation: 7 ERROR: invalid input syntax for type numeric: "3,5"
Run Code Online (Sandbox Code Playgroud)

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

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

我提出的唯一解决方法是非常丑陋和讨厌:

setlocale(LC_ALL, 'en_US');
$ae->setAmount(3.5);
setlocale(LC_ALL, 'de_DE');
Run Code Online (Sandbox Code Playgroud)

jak*_*lla 5

问题是Propel将与该列相关的PHP字段转换为setter中的本机PHP类型(如@j0k提到的那样),但是如果你看得更深一点就会看到问题.在PropelTypes.php帮助器类中,在第76行,您可以看到"decimal"的本机PHP类型被列为"string".

将其与"float"本机类型进行比较,该类型列为"double".我不确定这是否是故意的,但无论如何,只需将列切换为type="float"或可以解决您的问题type="double".

Propel应该将其转换为您的DBMS所需的任何类型.

  • 这是因为数据库中的DECIMAL字段是精确的,而CPU上的本机浮点处理则不是.因此,Propel通过返回字符串中的确切值来使其安全,并让您担心算术和存储.见fe [link](http://msdn.microsoft.com/en-us/library/c151dt3s%28v=vs.80%29.aspx) (3认同)