【发布时间】:2013-02-20 20:30:57
【问题描述】:
我有一个小问题,我真的不知道为什么。我正在使用 |date() 在树枝中打印日期时间变量,但它总是打印实际时间。
为了调试,我将以下代码放入我的模板中:
<pre>
{% debug entity.getCreatedAt|date("d.m.Y H:i:s") %}
{% debug entity.getCreatedAt|raw %}
{% debug entity.CreatedAt|raw %}
{% debug entity.CreatedAt|date("d.m.Y H:i:s") %}
我的变量被称为 CreatedAt,所以通常我应该使用 entity.CreatedAt|date("d.m.Y H:i:s") 获得正确的输出,对吗?
我的调试输出如下:
string '16.01.2013 13:46:03' (length=19) //entity.getCreatedAt|date("d.m.Y H:i:s")
object(DateTime)[4611] //entity.getCreatedAt|raw
public 'date' => string '2013-01-16 13:46:03' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Berlin' (length=13)
object(DateTime)[4938] //entity.CreatedAt|raw
public 'date' => string '2013-02-20 21:46:53' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Berlin' (length=13)
string '20.02.2013 21:46:53' (length=19) //entity.CreatedAt|date("d.m.Y H:i:s")
我不明白为什么我一调用 CreatedAt 就为 NULL。并且在调试标记之外它始终为 NULL,不取决于写作。
在我的实体中,我有:
private $CreatedAt;
public function setCreatedAt($createdAt)
{
$this->CreatedAt = $createdAt;
return $this;
}
public function getCreatedAt()
{
return $this->CreatedAt;
}
在 YML 中我得到了:
CreatedAt:
type: datetime
nullable: true
有人看错了吗??实在是没找到,可能是bug吧?
谢谢
【问题讨论】:
-
你使用
{{ entity.CreatedAt|date("d.m.Y H:i:s") }}有同样的结果吗? -
我已经测试过了,真的无法重现...你使用dump而不是`{% debug %}得到相同的结果吗?
-
你好 Cheesemacfly,我今天解决了我的问题 - 这是一个非常愚蠢的错误!我有一个名为 CreatedAt() 的 prePersist 类,Twig 当然在调用它 - 并收到 NULL .. 所以我将我的 prePersist 类重命名为 prePersist() 现在它工作得很好!
标签: symfony twig symfony-2.2