这要么是Symfony2中的一个巨大错误,要么就是我没有得到它。我花了几天的时间来了解这里到底发生了什么。我有两个实体:
Event
Date我想要一段有很多次约会的关系。听起来很简单,所以在我的事件实体中有:
/**
* @ORM\OneToMany(targetEntity="Date", mappedBy="event")
*/
protected $dates;在我的约会实体里我有:
/**
* @ORM\ManyToOne(targetEntity="Event", inversedBy="dates")
*/
private $event;我还在事件实体上生成了CRUD (原则:generate:crud),以便将事件添加到数据库中。在我的EventType中的表单生成器中,我添加了:
->add('date', new DateType())这样,我就可以按照Symfony文档在表单中包含日期字段。
我的问题来了。
每当我运行原则:生成:实体我的实体是在事件和日期实体上创建的,但它们似乎是错误的。在我的活动实体中,我得到:
/**
* Constructor
*/
public function __construct()
{
$this->dates = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add dates
*
* @param \Raygun\EventBundle\Entity\Date $dates
* @return Event
*/
public function addDate(\Raygun\EventBundle\Entity\Date $dates)
{
$this->dates[] = $dates;
return $this;
}
/**
* Remove dates
*
* @param \Raygun\EventBundle\Entity\Date $dates
*/
public function removeDate(\Raygun\EventBundle\Entity\Date $dates)
{
$this->dates->removeElement($dates);
}
/**
* Get dates
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getDates()
{
return $this->dates;
}在我的约会实体里我得到:
/**
* Set event
*
* @param \Raygun\EventBundle\Entity\Event $event
* @return Date
*/
public function setEvent(\Raygun\EventBundle\Entity\Event $event = null)
{
$this->event = $event;
return $this;
}
/**
* Get event
*
* @return \Raygun\EventBundle\Entity\Event
*/
public function getEvent()
{
return $this->event;
}现在,当我尝试加载表单时,我可以将事件/日期添加到数据库中。
Neither the property "date" nor one of the methods "getDate()", "date()", "isDate()", "hasDate()", "__get()" exist and have public access in class "Raygun\EventBundle\Entity\Event".它应该将getter和setter添加到事件实体中,而不是日期实体。我真的把我的头发弄得乱七八糟,我正在考虑完全抛弃Symfony,因为它似乎完全违背了逻辑。
发布于 2014-12-30 15:32:50
如果希望表单组件自动映射字段,则应将date字段更改为dates
->add('dates', 'collection', [
'type' => new DateType()
])或者,可以将mapped => false选项添加到date字段中,以手动映射它。
发布于 2014-12-30 15:36:03
事件窗体类型应该包含protected $dates字段的集合类型,因此这一行不正确:
->add('date', new DateType())它应该是:
->add('dates', 'collection', array('type' => new DateType()))请看一下关于如何使用表单集合的Symfony烹饪书条目:
collections.html
https://stackoverflow.com/questions/27707606
复制相似问题