本文介绍了在Symfony2中更新实体时的关系 - 一对一和一对多不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单中更新一个实体(它是反向方)有问题,而实体正确地更新了所有的数据,与其他实体相关,即在数据库中,它们的列引用了main 实体保持为空或数据保持不变。

I've a problem with updating an entity(it is inversed side) in form, while the entity is properly updated with all the data, the related to it other entities are not, i.e in database their column referencing the "main" entity remain null or the data remain untouched.

这是代码:

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,没有成功。
我会感谢任何建议。

According to my research and reading similiar problems with documentation it should work as is, however it doesn't, i suspect some basic misunderstanding on my part of how these relations should behave, work and be defined, but to this moment no bueno and no succes.I'd be grateful for any advice.

编辑 - OfferFormBuilder:

Edit - 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')
    ;
}


推荐答案

null表示你是不能正确地交叉引用对象。这是一个非常常见的错误。

The null indicates that you are not cross referencing the objects properly. It's a very common mistake.

class Offer {
  public function setEvent($event) {
    $this->event = $event;
    $event->setOffer($this); // *** This is what you are probably missing.

为交易也一样。

这篇关于在Symfony2中更新实体时的关系 - 一对一和一对多不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 10:20