ID 未保存在 OneToMany 关系中

ID not saved in OneToMany relationship

我正在使用 Symfony2 开发一个项目。但是我 运行 遇到了问题。我有一个实体 Purchase 和另一个 TypePurchase。它们具有一对多关系,但是当我查看 TypePurchase 的 table 内部时,未保存 Purchase 实体 ID。它的值为空。我已经在 addType 方法 Purchase 中尝试了 $types->setPurchase($this) 。但这给我的结果是只有第一个添加的实体得到一个 ID,下一个又是空的。

这是我在采购中的字段和 ID 字段:

/**
 * @ORM\OneToMany(targetEntity="TypePurchase", mappedBy="purchase", cascade={"persist", "remove" })
 */
protected $types;

这是我已经添加的,但没有完全发挥作用:

/**
 * Add types
 *
 * @param \Wood\AdminBundle\Entity\TypePurchase $types
 * @return Purchase
 */
public function addType(\Wood\AdminBundle\Entity\TypePurchase $types)
{
    $this->types[] = $types;
    $types->setPurchase($this);
    return $this;
}

这是我在 TypePurchase 中的字段:

/**
 * @ORM\ManyToOne(targetEntity="Purchase", inversedBy="types", cascade={"persist", "remove" })
 * @ORM\JoinColumn(referencedColumnName="id")
 */
protected $purchase;

已编辑: 购买中我的 ID 字段

/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

这是我在 Purchase 控制器中的 createAction:

/**
 * @param Request $request
 * @return \Symfony\Component\HttpFoundation\Response
 */
public function createAction(Request $request)
{
    $purchase = new Purchase();
    $purchase->addType(new TypePurchase());
    $form = $this->createForm(new PurchaseType(), $purchase);

    $form->handleRequest($request);
    if($form->isValid()) {
        $purchase->setUser($this->getUser());
        $purchaseService = $this->get('woodadmin.purchaseservice');
        $purchaseService->createPurchase($purchase);

        $flash = $this->get('braincrafted_bootstrap.flash');
        $flash->success('De inkoop is succesvol geregistreerd.');

        $now = time();
        if($purchase->getSupplier() != null) {
            $date = $purchase->getSupplier()->getFscExpiration()->format('U');
            $datediff = $date - $now;
            $diff = floor($datediff / (60 * 60 * 24));

            if ($diff <= 14) {
                $flash->alert('Let op, het FSC-certificaat van de leverancier ' . htmlentities($purchase->getSupplier()->getName()) . ' verloopt bijna of is al verlopen.');
            }

            $now2 = time();
            $date2 = $purchase->getSupplier()->getPefcExpiration()->format('U');
            $datediff2 = $date2 - $now2;
            $diff2 = floor($datediff2 / (60 * 60 * 24));

            if ($diff2 <= 14) {
                $flash->alert('Let op, Het PEFC-certificaat van de leverancier ' . htmlentities($purchase->getSupplier()->getName()) . ' verloopt bijna of is al verlopen.');
            }
        }

        return $this->redirect($this->generateUrl('wood_admin_purchase_list'));
    }
    return $this->render('WoodAdminBundle:Purchase:create.html.twig', array('title' => 'Inkoop registreren', 'page' => 'purchases', 'form' => $form->createView()));
}

我持久化 Purchase 实体的 PurchaseService 部分:

public function createPurchase(\Wood\AdminBundle\Entity\Purchase $purchase) {
    $this->entityManager->persist($purchase);
    $this->entityManager->flush();
}

在持久化 Purchase 实体之前,您需要先持久化 TypePurchase 实体。

The following example is an extension to the User-Comment example of this chapter. Suppose in our application a user is created whenever he writes his first comment. In this case we would use the following code:

<?php
$user = new User();
$myFirstComment = new Comment();
$user->addComment($myFirstComment);

$em->persist($user);
$em->persist($myFirstComment);
$em->flush();

Even if you persist a new User that contains our new Comment this code would fail if you removed the call to EntityManager#persist($myFirstComment). Doctrine 2 does not cascade the persist operation to all nested entities that are new as well.

http://doctrine-orm.readthedocs.org/en/latest/reference/working-with-associations.html

$purchase = new Purchase();
$typePurchase = new TypePurchase();
$this->enityManager->persist($typePurchase);
$purchase->addType($typePurchase);
$form = $this->createForm(new PurchaseType(), $purchase);