如何在 Symfony 3 中正确存储日期时间?
How do I properly store a datetime in Symfony 3?
我试图在 Symfony 存储库中存储 mysql 日期时间,但出现错误。尝试了来自网络和此处堆栈的一些建议,但没有任何方法可以消除此错误。这就是我想要做的(为了清楚起见,代码被缩写)
我的实体字段:
/**
* @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 日期时间中,但无论我尝试了什么,此错误都会不断出现。有人吗?
创建新的 DateTime 对象时不需要使用 'NOW'。您可以简单地使用 $now = new \DateTime()
实际 date/time.
对于您的情况 - 完全 o.k。要创建 DateTime 对象,请通过添加 XYdays 对其进行修改,因此:
$reservedays = 84;
$now = new \DateTime();
$now->modify('+' . $reservedays .' days');
但是你应该使用 DateTime 对象作为 setCreated()
方法参数,因为 $created
属性 有 \DateTime 类型。 Symfony 中的 Doctrine 层负责将适当的数据持久化到数据库,所以这应该可以正常工作:
$payment = new AppBundle\Entity\Payment;
$payment->setCreated($now);
您正试图在 datetime
列中保存 string
。停止调用 format() // <- Returns a string
即可。
此外,您可以通过在付款对象的 constructor method 中初始化 $createdAt
来简化一切。每当调用 class 的新实例时,都会调用构造函数。您还可以将变量传递给构造函数。
例如
// AppBundle\Entity\Payment
//...
class Payment
{
//...
// Set a default value for the passed-in variable.
public function __construct($reserveDays = 0)
{
$this->createdAt = new \DateTime('+'.$reserveDays.' days');
}
}
用法
// AppBundle\Controller\PaymentController.php
//...
$payment = new AppBundle\Entity\Payment(84);
我试图在 Symfony 存储库中存储 mysql 日期时间,但出现错误。尝试了来自网络和此处堆栈的一些建议,但没有任何方法可以消除此错误。这就是我想要做的(为了清楚起见,代码被缩写)
我的实体字段:
/**
* @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 日期时间中,但无论我尝试了什么,此错误都会不断出现。有人吗?
创建新的 DateTime 对象时不需要使用 'NOW'。您可以简单地使用 $now = new \DateTime()
实际 date/time.
对于您的情况 - 完全 o.k。要创建 DateTime 对象,请通过添加 XYdays 对其进行修改,因此:
$reservedays = 84;
$now = new \DateTime();
$now->modify('+' . $reservedays .' days');
但是你应该使用 DateTime 对象作为 setCreated()
方法参数,因为 $created
属性 有 \DateTime 类型。 Symfony 中的 Doctrine 层负责将适当的数据持久化到数据库,所以这应该可以正常工作:
$payment = new AppBundle\Entity\Payment;
$payment->setCreated($now);
您正试图在 datetime
列中保存 string
。停止调用 format() // <- Returns a string
即可。
此外,您可以通过在付款对象的 constructor method 中初始化 $createdAt
来简化一切。每当调用 class 的新实例时,都会调用构造函数。您还可以将变量传递给构造函数。
例如
// AppBundle\Entity\Payment
//...
class Payment
{
//...
// Set a default value for the passed-in variable.
public function __construct($reserveDays = 0)
{
$this->createdAt = new \DateTime('+'.$reserveDays.' days');
}
}
用法
// AppBundle\Controller\PaymentController.php
//...
$payment = new AppBundle\Entity\Payment(84);