我的实体:
/**
* @var \DateTime $publishedAt
*
* @ORM\Column(name="published_at", type="date")
*
* @Assert\Date()
*/
private $publishedAt;
/**
* Set publishedAt
*
* @param \DateTime $publishedAt
* @return MagazineIssue
*/
public function setPublishedAt(\DateTime $publishedAt)
{
$this->publishedAt = $publishedAt;
return $this;
}
/**
* Get published_at
*
* @return \DateTime
*/
public function getPublishedAt()
{
return $this->publishedAt;
}
我的表单生成器:
$builder->add('publishedAt');
我的观点:
{{ form_widget(form) }}
当我在选择中选择日期并提交表单时,我发现了错误:
Catchable Fatal Error: Argument 1 passed to ... must be an instance of DateTime,
string given, called in .../vendor/symfony/symfony/src/Symfony/Component/Form
/Util/PropertyPath.php on line 537 and defined in ... line 214
为什么会发生?如果我将字段设置器替换为public function setPublishedAt($publishedAt)
我收到错误:
Fatal error: Call to a member function format() on a non-object
in .../vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/DateType.php on line 44
如果我将表单生成器更改为
$builder->add('publishedAt','date')
一切正常。为什么会发生?为什么 symfony 无法猜测它并将正确的日期格式(\DateTime 而不是字符串)传递给字段设置器?
编辑:如果我删除@Assert\Date()
然后一切正常。我认为这是猜测日期字段类型的 sf2.1 错误