-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Closed
Description
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
Labels
No labels