Doctrine 2: Call to a member function format() on a non-object ... in DateTimeType.php

Jiew Meng picture Jiew Meng · Jul 31, 2010 · Viewed 82.2k times · Source

I have a DateTime field:

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

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');

I am getting the following error:

Fatal error: Call to a member function format() on a non-object in D:\ResourceLibrary\Frameworks\Doctrine\lib\Doctrine\DBAL\Types\DateTimeType.php on line 46

Answer

romanb picture romanb · Jul 31, 2010

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.