致命错误:在 Doctrine 中的非对象上调用成员函数 format()


Fatal error: Call to a member function format() on a non-object in Doctrine

我在 DOCTRINE 中使用日期作为数据类型,但它给了我这个错误:

Fatal error: Call to a member function format() on a non-object in C:'xampp'htdocs'test'doctrine'vendor'doctrine'dbal'lib'Doctrine'DBAL'Types'DateType.php on line 53

这是我的代码:

 /** 
 * private Date datePosted 
 * @Column(type="date")
 * @Assert'NotEmpty
 */
   private $datePosted ;

当我将类型更改为字符串时,它工作正常。我该如何解决这个问题?

您的注释是错误的。

试试这个:

/**
 * @var 'DateTime
 * @ORM'Column(type="date", nullable=false)
 */
 private $datePosted;

希望这有帮助。

编辑

您必须更新您的吸气剂和二传手(更改实体的YourEntityClass

/**
 * Set datePosted
 *
 * @param 'DateTime $datePosted
 * @return YourEntityClass
 */
public function setDatePosted($datePosted)
{
    $this->datePosted = $datePosted;
    return $this;
}
/**
 * Get datePosted
 *
 * @return 'DateTime 
 */
public function getDatePosted()
{
    return $this->datePosted;
}