【发布时间】:2017-08-10 09:17:51
【问题描述】:
我正在尝试将 mysql 日期时间存储在 Symfony 存储库中,但出现错误。尝试了来自网络和堆栈的几个建议,但没有什么能让这个错误消失。这就是我想要做的(为了清楚起见,代码被缩写)
我的实体字段:
/**
* @var \DateTime
*
* @ORM\Column(name="created", type="datetime")
*/
private $created;
我的回购代码:
$reservedays = 84;
$now = new \DateTime('NOW');
$now->modify('+' . $reservedays .' days');
$payment = new AppBundle\Entity\Payment;
$payment->setCreated( $now->format('Y-m-d h:i:s') );
但我一直收到此错误:
Error: Call to a member function format() on string
500 Internal Server Error - FatalErrorException
Stack Trace:
1. in vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/DateTimeType.php at line 53 -
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
return ($value !== null)
? $value->format($platform->getDateTimeFormatString()) : null;
}
/**
如您所见,我想获取当前日期并添加 84 天,然后将其存储到 mysql 日期时间中,但无论我尝试了什么,此错误都会不断出现。有人吗?
【问题讨论】:
-
传递实际的日期时间对象:$payment->setCreated($now);
-
$now 根据其调用 format() 函数是一个字符串。那不应该。尝试在您的回购中取出 NOW 作为参数
标签: php mysql datetime doctrine symfony3.x