我试图使用日期作为主键来创建一个实体。问题是,Symfony不能将我使用的DateTime转换为字符串,以便在IdentityMap中引入它。在实体持久化过程中,我得到以下错误:
Catchable Fatal Error: Object of class DateTime could not be converted to string in..
我在实体中使用这段代码
/**
* @ORM\Id
* @ORM\Column(type="datetime")
*/
protected $date;
错误出现在实体存储库中。
$em = $this->getEntityManager();
$currentData = new CurrentData();
...
$currentData->setDate(new \DateTime($dateStr));
...
$em->persist($currentData);
$em->flush();
我该如何解决这个问题?谢谢。
发布于 2013-06-15 17:17:23
你也许应该用一个正常的顺序。
但是,如果必须将日历信息用于键,则可能需要将其存储为“string”类型,然后使用php的DateTime格式:
$currentData->setDate(new \DateTime($dateStr)->format('yyyy/mm/dd');
如果您尝试使用datetime对象作为键,您可能会陷入困境。
https://stackoverflow.com/questions/17125863
复制相似问题