我在表单中更新实体(它是反向的)时遇到问题,而实体使用所有数据正确更新,与其他实体不相关,即在数据库中他们的列引用了"主"实体保持为空或数据保持不变。
以下是代码:
class Offer
{
/**
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity="DealOption", mappedBy="offer", cascade={"persist"})
*/
private $dealOptions;
/**
* @var Event
*
* @ORM\OneToOne(targetEntity="Event", inversedBy="offer")
* @ORM\JoinColumn(name="event_id", referencedColumnName="id")
*/
private $event;
}
class DealOption
{
/**
* @var Offer
*
* @ORM\ManyToOne(targetEntity="Offer", inversedBy="dealOptions")
* @ORM\JoinColumn(name="offer_id", referencedColumnName="id", onDelete="SET NULL")
*/
private $offer;
}
class Event
{
/**
* @var Offer
*
* @ORM\OneToOne(targetEntity="Offer", mappedBy="event")
* @ORM\JoinColumn(name="offer_id", referencedColumnName="id", onDelete="SET NULL")
*/
private $offer;
更新操作:
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('AppBundle:Offer')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Offer.');
}
$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);
if ($editForm->isValid()) {
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('offer'));
}
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
根据我的研究和阅读类似的文档问题,它应该按原样运行,但它不会,我怀疑我对这些关系应如何表现,工作和定义有一些基本的误解,但对此一刻没有bueno,没有成功。 我很感激任何建议。
编辑 - OfferFormBuilder:
class OfferType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('dealOptions')
->add('title')
->add('event')
->add('division')
;
}
答案 0 :(得分:1)
null表示您没有正确交叉引用对象。这是一个非常常见的错误。
class Offer {
public function setEvent($event) {
$this->event = $event;
$event->setOffer($this); // *** This is what you are probably missing.
对交易也这样做。