【问题标题】:Decimal return string in DoctrineDoctrine 中的十进制返回字符串
【发布时间】:2019-02-11 04:37:16
【问题描述】:

我有一个像这样的十进制字段

 /**
 *
 * @ORM\Column(name="foo", type="decimal", precision=0, scale=2, nullable=false, unique=false)
 */
private $foo;

当我使用 getFoo()QueryBuilder 时,我的值是“159.79”而不是 159.79。 我怎样才能得到正确的类型?

【问题讨论】:

标签: symfony doctrine


【解决方案1】:

即使您在 Doctrine 注释中将其声明为小数,当它被检索时,PHP 也会将其视为字符串。
Reference here.

从数据库中检索的值总是转换为 PHP 的 字符串类型,如果没有数据则为 null。

您可以将 getter 更改为浮点数,然后返回。
例如

public function getFoo()
{
    return (float) $this->foo;
}

public function getFoo()
{
    return floatval($this->foo);
}

查看Type Juggling 了解更多信息。
更多信息floatVal()here

【讨论】:

  • 是的,但如果我使用 querybuilder 我不使用 getFoo()
猜你喜欢
  • 2013-02-11
  • 2016-11-07
  • 2018-01-16
  • 1970-01-01
  • 2011-07-22
  • 2021-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多