Symfony2将实体列映射到另一个类

时间:2015-03-02 17:28:00

标签: php symfony date datetime

我是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的。

我打算做一个新的类,它会考虑我在parameters.yml中指定的日期时间格式

1 个答案:

答案 0 :(得分:0)

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;
相关问题