我有需要插入到数据库中的日期值。如果我尝试添加一个简单的字符串:
$user->setUpdatedAt('2015-05-31 05:46:23')ofc I得到错误,该值必须是DateTime对象。
因此,我创建了datetime对象:
$user->setUpdatedAt(new \DateTime($row[29]))这就产生了:
object(DateTime)[1052]
public 'date' => string '2015-05-31 05:46:23' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Paris' (length=12)但当我想冲水的时候我得到了:
调用非对象上的成员函数格式()
为什么它没有看到它是一个对象,当它清楚的时候呢?
更新:
实体类I--一个简单的FOSUser生成的实体:
/**
* @var \DateTime
*/
protected $createdAt;
/**
* @var \DateTime
*/
protected $updatedAt;
/**
* Sets the last update date.
*
* @param \DateTime|null $updatedAt
*
* @return User
*/
public function setUpdatedAt(\DateTime $updatedAt = null)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Returns the last update date.
*
* @return \DateTime|null
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* Hook on pre-persist operations.
*/
public function prePersist()
{
$this->createdAt = new \DateTime();
$this->updatedAt = new \DateTime();
}发布于 2016-07-27 13:56:33
原则的日期类型将传入值(当然,如果将字段类型设置为日期、日期时间、日期时间)转换为字符串,并在从db获取时将它们转换回datetime对象。尝试找到正确的类(DateTimeType.php、DateTimeTzType.php、DateType.php,基于您的映射类型)并调试convertToDatabaseValue方法(例如:dump($value);die();),检查即将出现的值。或者使用xdebug,从setter调试到convertToDatabaseValue方法。
https://stackoverflow.com/questions/38613729
复制相似问题