3

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

In controller

removeExpireProducts();
$em->flush();

Inside my service:

function removeExpireProducts()
{
    foreach ($products as $product) {
         $this->productRemover->removeProduct($product);
    }
}

productRemover class:
public function removeProduct(Product $product)
{

    $newOffer =
        $this->offerGroup
        ->createOffer($product);

    if (null !== $newOffer) {
        $this->em->persist($newOffer);
        $this->em->flush();
    }

    $this->em->remove($product);
}
Ricky ponting
  • 4,537
  • 4
  • 13
  • 26

1 Answers1

3

Move flush() to the bottom like this:

public function removeProduct(Product $product)
{
    $newOffer =
        $this->offerGroup
        ->createOffer($product);

    if (null !== $newOffer) {
        $this->em->persist($newOffer);
    }

    $this->em->remove($product);
    $this->em->flush();
}

And in you controller remove $em->flush();

Than refactor your code to avoid executing flush() in this loop:

foreach ($products as $product) {
     $this->productRemover->removeProduct($product);
}
Łukasz D. Tulikowski
  • 1,440
  • 1
  • 17
  • 36