简体   繁体   English

原则-事件监听器(preUpdate)不更新相关实体

[英]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. 在我的班级电子邮件中,我尝试通过EventListener(preUpdate或其他)检查是否只有一封电子邮件的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. 当我使用default = true(错误)更新电子邮件时,该电子邮件在db中已很好地更新,而其他电子邮件也保持default = true。 Or in my PreUpdate function (which is well called --> I can see it with my var_dump), I persist email with default=false. 或在我的PreUpdate函数(即->我可以用var_dump看到它)中,我使用default = false保留电子邮件。 But no changes in db for these emails. 但是这些电子邮件的数据库没有变化。 What's wrong ? 怎么了 ?

you are missing 你失踪了

$entityManager->flush();

after the persist 坚持之后

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM