Take the 2-minute tour ×

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

share|improve this question

3 Answers 3

up vote 33 down vote accepted

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.

share|improve this answer
you are right. i shld use date_create(date(...)) or DateTime::__construct() –  Jiew Meng Aug 1 '10 at 3:25
@JiewMeng: great solution, thanks! –  Ron Dec 28 '12 at 13:37

you could always use:

$this->updated = new \DateTime("now");


share|improve this answer
or put use \DateTime; at the top of the file and then $this->updated = new DateTime("now"); –  redolent May 6 at 17:25

I came across a similar problem, but with a time field, and this question and @romanb 's answer helped.

I was getting the following error, much like the one in the question.

... /vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/TimeType.php on line 50

The solution was similar, for fields of the time datatype, Doctrine will accept an instance of PHP's DateInterval

$quizFixture1->setCompletionTime(\DateInterval::createFromDateString('743 seconds'));
share|improve this answer

Your Answer


