我有一个具有以下属性的 News
实体:
- 文本
- 开始日期
- 过期日期
以下是类(class)的一部分:
//News.php
/**
* @ORM\Column(type="string", length=200)
*/
private $text;
/**
* @ORM\Column(type="datetime")
*/
private $start_date;
/**
* @ORM\Column(type="datetime")
*/
private $expire_date;
我想在 Twig 模板的列表中显示所有新闻,这就是我的做法:
<div class="list-group">
{%for news in news%}
<div class="panel panel-default">
<div class="panel-heading">{{ news.start_date|date('Y-m-d') }}</div>
<div class="panel-body">
{{ news.text }}
</div>
</div>
{%endfor%}
</div>
事实是我收到以下错误:
Neither the property "start_date" nor one of the methods "start_date()", "getstart_date()"/"isstart_date()" or "__call()" exist and have public access in class "AppBundle\Entity\News".
但是如果我尝试仅渲染 text
属性,我就能够访问它。
这是为什么?
最佳答案
您的属性是私有(private)的。你可以添加一个getter
public fucntion getStartDate() {
return $this->start_date;
}
在 Twig 中
{{ news.startDate|date('Y-m-d') }}
关于symfony - 无法访问 Twig 中的每个对象属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41890143/