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 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝