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.