0

I am using date as a Data-Type in DOCTRINE , but its giving me this error:

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

Here is my code:

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

When i change type to string ,than it works fine. How can i fix this?

Hassan Sardar
  • 4,413
  • 17
  • 56
  • 92
  • 1
    possible duplicate of [Doctrine 2: Call to a member function format() on a non-object ... in DateTimeType.php](http://stackoverflow.com/questions/3378748/doctrine-2-call-to-a-member-function-format-on-a-non-object-in-datetimety) – Udan Oct 21 '13 at 11:37
  • 1
    see this post http://stackoverflow.com/questions/3378748/doctrine-2-call-to-a-member-function-format-on-a-non-object-in-datetimety – Osama Jetawe Oct 21 '13 at 11:39
  • Basically I didnt helped my, thats why I asked question, Can anyone provide me an answer ? – Hassan Sardar Oct 21 '13 at 11:42
  • can you please post the code where you set the date field's value? – Udan Oct 21 '13 at 11:47

1 Answers1

4

Your annotation is wrong.

Try with this:

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

Hope this helps.

EDIT

You have to update your getter and setter (change YourEntityClass for your Entity)

/**
 * 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;
}