Symfony Error: Call to a member function format() on string

Screen Shot 2015-11-27 at 11.53.37 AMThis Symfony error tripped me up for a few minutes today, it’s not particularly obvious what it’s referring to.

Error: Call to a member function format() on string

In my case, I was setting a Doctrine entity field to a string when it should have been a DateTime object. In my entity class I defined a member variable like this.

/**
 * @ORM\Column(type="date")
 */
protected $date;

But when I was calling setDate() I was using a string rather than a DateTime() object and so I got this error. So if you see this error, check how you’re building out your entity. The error won’t come up when you’re calling setDate(), but rather when you’re trying to persist the entity to your database.

Share Button

Leave a Reply

Your email address will not be published.