Pierre Olivier Tran
Pierre Olivier Tran

Reputation: 1879

Symfony2 Datetime format

I'm trying to add a simple comment entity, with the "postedOn" attribute as a Datetime. Whenever I try to add a comment, I get this error :

Error: Call to undefined method Symfony\Component\Validator\Constraints\DateTime::format() in /var/www/vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/DateTimeType.php line 53

Any idea ?

Here's the entity code :

<?php

namespace AOFVH\FlyBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Comment
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="AOFVH\FlyBundle\Entity\CommentRepository")
 */
class Comment
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var integer
 *
 * @ORM\Column(name="rating", type="integer")
 */
private $rating;

/**
 * @ORM\Column(type="datetime", nullable=true)
 */
protected $postedon;

/**
 * @var string
 *
 * @ORM\Column(name="text", type="text")
 */
private $text;

/**
 * @var $commenter
 *
 * @ORM\ManyToOne(targetEntity="AOFVH\UserBundle\Entity\User", inversedBy="comments", cascade={"persist", "merge"})
 */
private $commenter;


/**
 * @var $commenter
 *
 * @ORM\ManyToOne(targetEntity="AOFVH\FlyBundle\Entity\Flight", inversedBy="comments", cascade={"persist", "merge"})
 */
private $flight;

/**
 * Get id
 *
 * @return integer
 */
public function getId()
{
    return $this->id;
}

/**
 * Set rating
 *
 * @param integer $rating
 * @return Comment
 */
public function setRating($rating)
{
    $this->rating = $rating;

    return $this;
}

/**
 * Get rating
 *
 * @return integer
 */
public function getRating()
{
    return $this->rating;
}

/**
 * Set text
 *
 * @param string $text
 * @return Comment
 */
public function setText($text)
{
    $this->text = $text;

    return $this;
}

/**
 * Get text
 *
 * @return string
 */
public function getText()
{
    return $this->text;
}

/**
 * Set commenter
 *
 * @param \AOFVH\UserBundle\Entity\User $commenter
 * @return Comment
 */
public function setCommenter(\AOFVH\UserBundle\Entity\User $commenter = null)
{
    $this->commenter = $commenter;

    return $this;
}

/**
 * Get commenter
 *
 * @return \AOFVH\UserBundle\Entity\User
 */
public function getCommenter()
{
    return $this->commenter;
}

/**
 * Set flight
 *
 * @param \AOFVH\FlyBundle\Entity\Flight $flight
 * @return Comment
 */
public function setFlight(\AOFVH\FlyBundle\Entity\Flight $flight = null)
{
    $this->flight = $flight;

    return $this;
}

/**
 * Get flight
 *
 * @return \AOFVH\FlyBundle\Entity\Flight
 */
public function getFlight()
{
    return $this->flight;
}

Here are the postedOn getters and setters

/**
 * Set postedon
 *
 * @param \DateTime $postedon
 * @return Comment
 */
public function setPostedon($postedon)
{
    $this->postedon = $postedon;

    return $this;
}

/**
 * Get postedon
 *
 * @return \DateTime
 */
public function getPostedon()
{
    return $this->postedon;
}
}

Upvotes: 1

Views: 3858

Answers (1)

Charlie Lucas
Charlie Lucas

Reputation: 290

I think it's your mapping in your entity, you don't use "datetime" doctrine format else your form type will try to generate a date with a string for example but for me "postedOn must always be defined on a new comment.You can update your entity constructor with : $this->setPostedOn(new \Datetime()); or you can use TimestampableTrait

Upvotes: 1

Related Questions