Symfony 2细枝

编程入门 行业动态 更新时间:2024-10-27 11:21:53
本文介绍了Symfony 2细枝| date()不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个小问题,我真的不知道为什么.我正在使用| date()在细枝中打印日期时间变量,但始终打印实际时间.

i have a little problem and I really don't know why. I am printing a datetime variable in twig with |date() but it is allways printing the actual time.

为了进行调试,我在模板中添加了以下代码:

for Debugging I put the following Code in my Template:

<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")获得正确的输出,对吗?

My Variable is called CreatedAt so normally I should get the correct output with entity.CreatedAt|date("d.m.Y H:i:s"), right?

我的Debug输出如下:

My Debug output is as follows:

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,而不取决于编写内容.

I don't understand why it is NULL as soon as I call CreatedAt. And OUTSIDE of the debug tag it is ALLWAYS NULL, not depending on the writing.

在我的实体中,我有:

private $CreatedAt; public function setCreatedAt($createdAt) { $this->CreatedAt = $createdAt; return $this; } public function getCreatedAt() { return $this->CreatedAt; }

在YML中,我得到了:

And in the YML I've got:

CreatedAt: type: datetime nullable: true

有人看到错误了吗?我真的找不到它,也许是个错误?

Does anybody see a mistake?? I really don't find it, maybe it is a bug?

谢谢

推荐答案

如果您想在实体类之外访问您的 private 变量$CreatedAt,则必须调用 public 吸气剂方法getCreatedAt()(这就是这里的用途).

If you want to access your private variable $CreatedAt outside your entity class, you have to call the public getter method getCreatedAt() (that's what it's here for).

在树枝模板中,调用{% debug entity.CreatedAt|date("d.m.Y H:i:s") %}时,由于entity.CreatedAt是NULL,所以返回的字符串基于新的日期对象:

And in your twig template, when you call {% debug entity.CreatedAt|date("d.m.Y H:i:s") %}, since entity.CreatedAt is NULL, the returned string is based on a new date object:

如果传递给日期过滤器的值为null,则默认情况下它将返回当前日期.

If the value passed to the date filter is null, it will return the current date by default.

twig.sensiolabs/doc/filters/date.html

更新:

如@insertusername此处所述,twig自动调用公共获取程序. 但是,仅当在{% %}上使用定界符{{ }}时,才会出现这种情况.

As mentioned by @insertusernamehere, twig automatically calls the public getter. But this behavior seems to happen only when using the delimiters {{ }} over {% %}.

twig.sensiolabs/doc/templates.html#synopsis

更多推荐

Symfony 2细枝

本文发布于:2023-11-22 19:04:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618634.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:细枝   Symfony

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!