Symfony2日期时间格式

时间:2015-04-08 09:34:28

标签: symfony datetime

我正在尝试添加一个简单的注释实体,其中“postedOn”属性为Datetime。每当我尝试添加评论时,都会收到此错误:

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

有什么想法吗?

这是实体代码:

<?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;
}

以下是发布的getter和setter

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

1 个答案:

答案 0 :(得分:1)

我认为这是您在实体中的映射,您不会使用&#34; datetime&#34; doctrine格式,否则你的表单类型将尝试生成一个带字符串的日期,例如但对我来说,必须始终在新评论上定义postedOn。您可以使用:$this->setPostedOn(new \Datetime());更新您的实体构造函数,或者您可以使用TimestampableTrait