Skip to content

Entity listener preUpdate or postUpdate methods not called symfony 4.2 #11160

@chernoubi

Description

@chernoubi

i want to use entity listener on Site entity this is my code for Site.php

       namespace App\Entity;

    use Doctrine\Common\Collections\ArrayCollection;
    use Doctrine\Common\Collections\Collection;
    use Doctrine\ORM\Mapping as ORM;
    use Symfony\Component\Validator\Constraints as Assert;

    /**
     * @ORM\Entity(repositoryClass="App\Repository\SiteRepository")
     * @ORM\EntityListeners({"App\Entity\Listener\SiteListener"})
     * @ORM\HasLifecycleCallbacks()
     */
    class Site
    {
        /**
         * @ORM\Id()
         * @ORM\GeneratedValue()
         * @ORM\Column(type="integer")
         * @Assert\Type("int")(groups={"inputsite"})
         */
        private $id;

        /**
         * @ORM\Column(type="string", length=255)
         * @Assert\NotBlank(groups={"inputsite"})
         */
        private $intitule;
etc ...

SiteListener.php

<?php

namespace App\Entity\Listener;

use App\Entity\Site;
use App\Repository\SiteRepository;


class SiteListener
{

    public function prePersist(Site $site, LifecycleEventArgs $args)
    {
        echo 'prePersist';

    }

    public function preUpdate(Site $site, LifecycleEventArgs $args)
    {
      echo 'preUpdate';

    }

}

and my service is declared like


site_listener:
    class: App\Entity\Listener\SiteListener
    arguments: ~
    tags:
        - { name: doctrine.orm.entity_listener }

**when i try to insert new site its OK!

My probleme is when i want to update an existant site ,prePersist method is always called instead of preUpdate ,and nothing is updated and my site was saved like a new enregistement in BDD with a new id**
-If i remove the tag key from my service i can add/update my Site but no event is fired

/**
 * @Route("/default", name="default")
 */
public function index()
{


    $siteInput= $this->getDoctrine()->getRepository(Site::class)->find(169);
    $siteInput->setIntitule('setIntitule')->setDomaine('setDomaine')->setIsEnabled(true);

    $this->getDoctrine()->getManager()->persist($siteInput);
    $this->getDoctrine()->getManager()->flush();

    die('fin');
}

Thx :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions