我是symfony2的新手。我有以下问题:
我有一个名为Birthday的实体(我当然简化了实体)
class Birthday{
private $date;
//@return 'DateTime object
public function getDate(){
return $this->date;
}
public function __toString(){
return $this->date->format('IM AWARE IS NOT A GOOD IDEA: the format should be taken from parameters.yml');
}
}
我想要一个其他类型的对象,而不是''DateTime。我怎么能这么做?
我希望在BirthDay实体中有一个__toString方法,它将以取自parameters.yml.的格式显示日期
同样,我知道将服务容器传递到实体中绝对不是一个好主意,因为实体是POPO。
我计划做一个新的类,它将考虑我在参数中指定的日期-时间格式。yml
将format
属性添加到Birthday
类中,并在__toString
方法中调用它。。示例:实体:
public function __toString()
{
return $this->date->format($this->format);
}
控制器:
$format = $this->container->getParameter('FORMAT_FROM_PARAMETERS')
...
$birthday->setFormat($format);
...
print $birthday;
die;