【问题标题】:Fatal error: Call to a member function format() on a non-object in Doctrine [closed]致命错误:在 Doctrine 中的非对象上调用成员函数 format() [关闭]
【发布时间】:2013-10-29 21:06:59
【问题描述】:

我在 DOCTRINE 中使用 date 作为 Data-Type ,但它给了我这个错误:

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 ;

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

【问题讨论】:

标签: php oop symfony orm doctrine-orm


【解决方案1】:

你的注释是错误的。

试试这个:

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

希望这会有所帮助。

编辑

您必须更新您的 getter 和 setter(为您的实体更改 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;
}

【讨论】:

  • 现在它没有插入数据库
  • 如果我将它设置为字符串而不是插入,但是当我将它设置为 Date 时,它​​不会插入,
  • 我刚刚编辑了我的答案。此外,您必须更新数据库中的 datePosted 类型。
  • 你能告诉我你的ControllerAction吗?
  • 对于未来的访问者:如果您不关心小时和分钟,请仅使用@ORM\Column(type="date", ...),否则使用@ORM\Column(type="datetime", ...)
猜你喜欢
  • 2012-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多