Search code examples
phpdoctrineevent-listenerflushpersist

Doctrine - Event Listener (preUpdate) not updating related entities


I have a entity Client with one a linked entity Email.

class BusinessClientCustomer
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="firstname", type="string", length=255, nullable=true)
     */
    protected $firstname;

    /**
     * @var string
     *
     * @ORM\Column(name="lastname", type="string", length=255, nullable=true)
     */
    protected $lastname;

    /**
     * @var ArrayCollection
     *
     * @ORM\OneToMany(targetEntity="BusinessClientCustomerEmail", mappedBy="client_customer")
     */
    protected $emails;

In my class Email, I try to check via EventListener (preUpdate or others) that there is only one email with default=true.

class BusinessClientCustomerEmail
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="email", type="string", length=255)
     */
    protected $email;

/**
     * @var boolean
     *
     * @ORM\Column(name="`default`", type="boolean")
     */
    protected $default;

    /**
     * @var BusinessClientCustomer
     *
     * @ORM\ManyToOne(targetEntity="BusinessClientCustomer", inversedBy="emails")
     */
    protected $client_customer;

public function __prePersist(EntityManager &$entityManager, ContainerInterface &$container) {
        var_dump('prePersist');
    }

    public function __postPersist(EntityManager &$entityManager, ContainerInterface &$container) {
        var_dump('postPersist');
    }

    public function __preUpdate(EntityManager &$entityManager, ContainerInterface &$container) {
        var_dump('preUpdate');
        if ($this->default) {
            foreach ($this->client_customer->getEmails() as $email) {
                if ($email->getId() !== $this->id) {
                    $email->setDefault(false);
                    $entityManager->persist($email);
                }
            }
        } else if (count($this->client_customer->getEmails()) === 1) {
            $this->setDefault(true);
        } else {
            $default = true;
            foreach ($this->client_customer->getEmails() as $email) {
                if ($email->getDefault()) {
                    $default = false;
                }
            }
            if ($default) {
                $this->setDefault(true);
            }
        }
    }

    public function __postUpdate(EntityManager &$entityManager, ContainerInterface &$container) {
        var_dump('postUpdate');
    }

    public function __preFlush(EntityManager &$entityManager, ContainerInterface &$container) {
        var_dump('preFlush');
        if ($this->default) {
            foreach ($this->client_customer->getEmails() as $email) {
                $email->setDefault(false);
                $entityManager->persist($email);
            }
        } else if (count($this->client_customer->getEmails()) === 0) {
            $this->setDefault(true);
        }
    }

    public function __postFlush(EntityManager &$entityManager, ContainerInterface &$container) {
        var_dump('postFlush');
    }

    public function __onFlush(EntityManager &$entityManager, ContainerInterface &$container) {
        var_dump('onFlush');
    }

When I update an email with default=true (it was false), this email is well updated in db but the others emails keep default=true too. Or in my PreUpdate function (which is well called --> I can see it with my var_dump), I persist email with default=false. But no changes in db for these emails. What's wrong ?


Solution

  • you are missing

    $entityManager->flush();

    after the persist