Symfony原则一对多不插入外键 [英] Symfony Doctrine One to Many does not insert foreign key

查看:100
本文介绍了Symfony原则一对多不插入外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与一个或多个OneToMany-Childs合并实体时遇到烦人的问题。

I am having annoying problems with persisting an entity with one or more OneToMany-Childs.

我有一个 Buchung。实体可以具有多个 Einsatztage。 (可以翻译成很多天的事件)

I have a "Buchung" entity which can have multiple "Einsatztage" (could be translated to an event with many days)

在我拥有的蒲种实体

/**
 * @param \Doctrine\Common\Collections\Collection $property
 * @ORM\OneToMany(targetEntity="Einsatztag", mappedBy="buchung", cascade={"all"})
 */
private $einsatztage;

$ einsatztage在__constructor()中设置为ArrayCollection()。

$einsatztage is set to an ArrayCollection() in the __constructor().

然后是 Einsatztag具有$ Buchung_id变量以引用 Buchung的实体

Then there is the "Einsatztag" Entity which has a $Buchung_id variable to reference the "Buchung"

/**
 * @ORM\ManyToOne(targetEntity="Buchung", inversedBy="einsatztage", cascade={"all"})
 * @ORM\JoinColumn(name="buchung_id", referencedColumnName="id")
 */
private $Buchung_id;

现在,如果我尝试将对象持久化到数据库中,则 Einsatztag的外键会被删除。表始终始终为空。

Now If I try to persist an object to the database the foreign key of the "Einsatztag" Table is always left empty.

$ buchung = new Buchung();

$buchung = new Buchung();

$buchung->setEvent(         $r->request->get("event_basis"));
$buchung->setStartDate(new \DateTime($r->request->get("date_from")));
$buchung->setEndDate(new \DateTime($r->request->get("date_to")));


$von = $r->request->get("einsatz_von");
$bis = $r->request->get("einsatz_bis");
$i = 0;
foreach($von as $tag){
    $einsatztag = new Einsatztag();

    $einsatztag->setNum($i);
    $einsatztag->setVon($von[$i]);
    $einsatztag->setBis($bis[$i]);

    $buchung->addEinsatztage($einsatztag);
    $i++;
}

$em = $this->getDoctrine()->getManager();

$em->persist($buchung);

foreach($buchung->getEinsatztage() as $e){
    $em->persist($e);
}
$em->flush();


推荐答案

首先,您必须了解Doctrine和Symfony确实可以在您的实体中不能使用id。在Einsatztag实体中,您的媒体资源不应称为$ Buchung_id,因为它是buchung的实例,而不是您会在其中找到的ID。

Firstly, you have to understand that Doctrine and Symfony does not work with id's within your entities.In Einsatztag entity, your property should not be called $Buchung_id since it's an instance of buchung and not an id you will find out there.

此外,在循环中,将Einsatztag添加到Buchung。但是,您是否处理反向集?

Moreover, in your loop, you add the Einsatztag to Buchung. But do you process the reverse set ?

我这样做是为了始终反转实体的集/添加。

I do it this way to always reverse the set/add of entities.

Einsatztag

Einsatztag

public function setBuchung(Buchung $pBuchung, $recurs = true){
     $this->buchung = $pBuchung;

     if($recurs){
        $buchung->addEinsatztag($this, false);
     }
}

蒲种

public function addEinsatztag(Einsatztag $pEinsatztag, $recurs = true){
     $this->einsatztages[] = $pEinsatztag;

     if($recurs){
        $pEinsatztag->setBuchung($this, false);
     }
}

然后,何时呼叫

$buchung->addEinsatztag($einsatztag);

$einsatztag->set($buchung);

该关系将在双方都设置好,以便设置您的FK。请注意这一点,如果使用不当,您将有一些行为,例如重复输入。

The relation will be set on both side making your FK to be set. Take care of this, you'll have some behavior like double entries if you do not use them properly.

SImplier,您可以使用默认的getter / setter并调用它们关系的两面,使用您已有的东西,例如:

SImplier , you can use default getter/setters and call them on both sides of your relation, using what you already have, like following:

$einsatztag->set($buchung);
$buchung->addEinsatztag($einsatztag);

希望它有所帮助;)

这篇关于Symfony原则一对多不插入外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆