dshun123456 2014-07-17 11:48
浏览 70
已采纳

Symfony - doctrine PreUpdateEventArgs错误

I'm working on something with Sonata Admin and I'm trying to update an entity instance that has child instances associated to it and I get the following error.

ContextErrorException: Catchable Fatal Error: Argument 3 passed to Doctrine\ORM\Event\PreUpdateEventArgs::__construct() must be of the type array, null given, called in ROOT\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php on line 995 and defined in ROOT\vendor\doctrine\orm\lib\Doctrine\ORM\Event\PreUpdateEventArgs.php line 47

To put it in words, I'm trying to save an object called invoice and along with that I'm saving the order objects associated to the invoice.

Here is my code:

    $invoice = ....; //I get this as a parameter to the method
    $manager = $this->getDoctrine()->getManager();
    $invoice->setStatus('validated');
    foreach ($invoice->getOrders() as $order) {
        /** @var Order $order */
        $order->setOrderStatus('invoiced');
    }
    $manager->persist($invoice);
    $manager->flush();
    $manager->clear();

The $orders member is annotated as this

/**
 * @ORM\OneToMany(targetEntity="Order", mappedBy="invoice", cascade={"persist"}, orphanRemoval=false)
 */
 $orders;

The strange thing is, if I have only one $order object associated to the invoice everything runs smoothly, but when there are at least 2 everything goes boom.

After some debugging I saw that it is trying to save the second $order object twice and the second time it crashes.

I saw Symfony2 postUpdate listener not working. That one does not have answers and it looks like a different case to me (but not sure).

[EDIT]
It makes no difference if I have cascade={"persist"} or not.

  • 写回答

1条回答 默认 最新

  • douzhoubing2805 2014-07-17 12:14
    关注

    The issue seams to be fixed if I reload the Invoice object :

    $invoice = $manager->getRepository('BundleNameHere:Invoice')->findOneById($invoice->getId());
    

    But this is kind of strange, The object I receive as parameter for my method is identical to the one I get after reloading it sing the repository.

    I guess this will do for now, it solved my problem, but any ideas on why this happens are welcomed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要