原则 2:在 DateTimeType.php 中对非对象 ... 调用成员函数 format()

2022-01-16 00:00:00 php doctrine doctrine-orm

我有一个 DateTime 字段:

/**
 * Date time posted
 * @Column(type="datetime")
 */
private $dtPosted;

使用 LifeCycleCallback 设置为默认值

which is set to a default value by using a LifeCycleCallback

/**
 * @PrePersist
 */
function onPrePersist() {
    // set default date
    $this->dtPosted = date('Y-m-d H:m:s');

我收到以下错误:

致命错误:调用成员函数format() 在非对象上D:ResourceLibraryFrameworksDoctrinelibDoctrineDBALTypesDateTimeType.php第 46 行

Fatal error: Call to a member function format() on a non-object in D:ResourceLibraryFrameworksDoctrinelibDoctrineDBALTypesDateTimeType.php on line 46

推荐答案

date() 函数返回一个字符串.datetime 类型适用于 DateTime 对象.因此,要么将映射类型更改为 string,要么使用 DateTime 对象.

The date() function returns a string. The datetime type works with DateTime objects. So either change the mapping type to string or use DateTime objects.

相关文章