ID未保存在OneToMany关系中 [英] ID not saved in OneToMany relationship

查看:50
本文介绍了ID未保存在OneToMany关系中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Symfony2开发一个项目.但是我遇到了一个问题.我有一个实体Purchase,另一个是TypePurchase.它们具有OneToMany关系,但是当我查看TypePurchase表的内部时,不会保存购买实体ID.它的值为空.我已经在addType方法Purchase中尝试了$ types-> setPurchase($ this).但这给了我一个结果,只有第一个添加的实体获得一个ID,下一个再次为null.

I'm working on a project using Symfony2. But I'm running into a problem. I have an entity Purchase and another, TypePurchase. They have an OneToMany-relationship, but when I look inside of the table of TypePurchase, the Purchase entity ID is not saved. It's value is null. I already tried $types->setPurchase($this) in the addType method Purchase. But this gives me the result that only the first added entity get's an ID, the next one is again null.

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

This is my field and ID field in Purchase:

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

这是我已经添加的,但是不能完全正常工作:

This is what I already added, but is not working fully:

/**
 * 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中的字段:

And this is my field inside 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:

This is my createAction in the Purchase controller:

/**
 * @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()));
}

我的PurchasingService部分,在其中保留了Purchase实体:

My PurchaseService part where I persist the Purchase entity:

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

推荐答案

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

You need to persist your TypePurchase entity before you persist your Purchase Entity.

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

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

即使您保留包含我们新注释的新用户,如果您删除了对以下内容的调用,此代码也将失败EntityManager#persist($ myFirstComment).主义2 不级联对所有同时也是新的嵌套实体的持久操作.

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

这篇关于ID未保存在OneToMany关系中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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