Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony & Doctrine : UnitOfWork undefined index after calling flush()

I have 3 entities :

Folder :

<?php
namespace CMS\ExtranetBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Folder
 *
 * @ORM\Table(name="folder")
 * @ORM\HasLifecycleCallbacks
 * @ORM\Entity(repositoryClass="CMS\ExtranetBundle\Repository\FolderRepository")
 */
class Folder
{
    /**
     * @ORM\Id
     * @ORM\Column(name="id", type="guid")
     * @ORM\GeneratedValue(strategy="UUID")
     */
    public $id;
    // Used in NotificationListener
    public $beforeRemoveId;
    /**
     * @ORM\OneToMany(targetEntity="Document", mappedBy="folder", cascade={"persist", "remove"})
     */
    public $documents;
}

Document :

<?php
namespace CMS\ExtranetBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Document
 * @ORM\Entity(repositoryClass="CMS\ExtranetBundle\Repository\DocumentRepository")
 * @ORM\Table(name="document")
 * @ORM\HasLifecycleCallbacks
 */
class Document
{
    /**
     * @ORM\Id
     * @ORM\Column(name="id", type="guid")
     * @ORM\GeneratedValue(strategy="UUID")
     */
    public $id;
    /**
     * @ORM\ManyToOne(targetEntity="Folder", inversedBy="documents", cascade={"persist"})
     * @ORM\JoinColumn(name="folder_id", referencedColumnName="id", nullable=true)
     */
    public $folder;
}

and Notification :

<?php
namespace CMS\ExtranetBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table(name="notification")
 * @ORM\Entity(repositoryClass="CMS\ExtranetBundle\Repository\NotificationRepository")
 * @ORM\HasLifecycleCallbacks
 */
class Notification
{
    /**
     * @ORM\Id
     * @ORM\Column(name="id", type="guid")
     * @ORM\GeneratedValue(strategy="UUID")
     */
    public $id;
    /**
     * @ORM\ManyToOne(targetEntity="User", inversedBy="notifications", cascade={"persist"})
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id", nullable=true, onDelete="CASCADE")
     */
    public $user;
    /**
     * @ORM\Column(name="type", type="string", length=16)
     */
    public $type;
    /**
     * @ORM\Column(name="object_id", type="string", length=36)
     */
    public $objectId;
}

And last, I have a listener than "listen" to "Document" entities (preRemove and postRemove) in order to delete Notification entities. Notification entity isn't linked to Document with a relation because the field "objectId" is generic and it can contains other Entity, depending of the "type" attribute.

Here is my listener :

<?php
namespace CMS\ExtranetBundle\EventListener;

use Doctrine\ORM\Event\LifecycleEventArgs;

class NotificationListener
{
    /**
     * Enregistre l'ID avant la suppression pour l'utiliser dans le postRemove
     *
     * @param LifecycleEventArgs $args
     * @return bool|void
     */
    public function preRemove(LifecycleEventArgs $args)
    {
        $entity = $args->getEntity();
        $class  = (new \ReflectionClass($entity))->getShortName();
        if (!$this->isOfValidClass($class)) {
            return;
        }
        $entity->beforeRemoveId = $entity->getId();
    }
    /**
     * Lors de la suppression d'une entité, supprime les notifications correspondantes.
     *
     * @param LifecycleEventArgs $args
     * @return bool|void
     */
    public function postRemove(LifecycleEventArgs $args)
    {
        $entity = $args->getEntity();
        $class  = (new \ReflectionClass($entity))->getShortName();
        if (!$this->isOfValidClass($class)) {
            return;
        }
        $id = $entity->beforeRemoveId;
        if (!$id) {
            return;
        }

        $em = $args->getEntityManager();
        $notifications = $em->getRepository('CMSExtranetBundle:Notification')->findBy([
            'type'     => strtolower($class),
            'objectId' => $id
        ]);
        if (count($notifications)) {
            $batchSize = 20;
            $i = 1;
            foreach ($notifications as $notification) {
                $em->remove($notification);
                if (($i % $batchSize) === 0) {
                    $em->flush();
                    $em->clear();
                }
                ++$i;
            }
            // It fails after calling flush()
            $em->flush();
        }
    }
    private function isOfValidClass($class)
    {
        $allowedClasssNames = [
            'Document',
        ];
        foreach ($allowedClasssNames as $allowedClasssName) {
            if ($class == $allowedClasssName) {
                return true;
            }
        }
        return false;
    }
}

I'm trying to delete a Folder entity that contains multiples Document.

Here is my controller :

<?php
namespace CMS\ExtranetBundle\Controller;

use Symfony\Component\HttpFoundation\JsonResponse;
use CMS\ExtranetBundle\Entity\Folder;
use CMS\ExtranetBundle\Security\Voters\FolderVoter;

class FolderController extends DefaultController
{
    public function deleteAction(Request $request, Folder $folder)
    {
        // Authorization
        $this->denyAccessUnlessGranted(FolderVoter::WRITE, $folder);
        $em = $this->getDoctrine()->getManager();
        $em->remove($folder);
        $em->flush();
        return new JsonResponse([
            'status'      => true,
        ]);
    }
}

My problem is, sometimes, when I delete a Folder, that contain Documents, that have Notification, I got this error :

enter image description here

(Line 73 of NotificationListener.php correspond to the last $em->flush(); after the loop in postRemove)

If the Documents have no Notification, it works

Any idea ?

like image 500
Quentin L.D. Avatar asked Jul 06 '17 09:07

Quentin L.D.


1 Answers

Ok, thanks to srm` @ #symfony-fr on irc.freenode.net, I changed the way to delete Notification. Here is the postRemove() method of my NotificationListener :

/**
 * Lors de la suppression d'une entité, supprime les notifications correspondantes.
 *
 * @param LifecycleEventArgs $args
 * @return bool|void
 */
public function postRemove(LifecycleEventArgs $args)
{
    $entity = $args->getEntity();

    // Récupère le nom 'court' de la classe
    $class = (new \ReflectionClass($entity))->getShortName();

    if (!$this->isValidClass($class)) {
        return;
    }
    $id = $entity->beforeRemoveId;
    if (!$id) {
        return;
    }
    $em = $args->getEntityManager();

    $queryBuilder = $em
        ->createQueryBuilder()
        ->delete('CMSExtranetBundle:Notification', 'n')
        ->where('n.type = :type')
        ->andWhere('n.objectId = :objectIds')
        ->setParameter(':type', strtolower($class))
        ->setParameter(':objectIds', $entity->beforeRemoveId);

    $queryBuilder->getQuery()->execute();

}

Thanks ! :-D

like image 124
Quentin L.D. Avatar answered Oct 07 '22 17:10

Quentin L.D.