ID未保存在OneToMany关系中

时间:2015-03-03 10:53:31

标签: symfony doctrine one-to-many

我正在使用Symfony2开发一个项目。但我遇到了一个问题。我有一个实体购买和另一个,TypePurchase。它们具有OneToMany关系,但是当我查看TypePurchase表时,不会保存Purchase实体ID。它的值为null。我已经在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;

这是我在购买控制器中的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()));
}

我的PurchaseService部分,我坚持购买实体:

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

1 个答案:

答案 0 :(得分:0)

在您保留TypePurchase实体之前,您需要保留Purchase实体。

  

以下示例是User-Comment示例的扩展   本章。假设在我们的应用程序中,每当他创建用户时   写下他的第一条评论。在这种情况下,我们将使用以下内容   代码:

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

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

即使您持有包含我们新评论的新用户,如果您将调用删除,此代码也会失败   EntityManager的坚持#($ myFirstComment)。学说2 不级联   对所有嵌套实体的持久操作也是新的

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);