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

时间:2019-03-13 13:56:56

标签: php doctrine event-listener flush persist

我有一个实体客户,其中有一个链接的实体电子邮件。

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;

在我的电子邮件类中,我尝试通过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');
    }

当我使用default = true(错误)更新电子邮件时,该电子邮件在db中得到了很好的更新,但其他电子邮件也保持default = true。或在我的PreUpdate函数(即->我可以用var_dump看到它)中,我使用default = false保留电子邮件。但是这些电子邮件的数据库没有变化。怎么了?

1 个答案:

答案 0 :(得分:1)

您丢失了

gridBoundColumnBuilder = gridBoundColumnBuilder.Filterable(filterable => filterable.UI(handler) .Extra(false) .Operators(configurator => configurator .ForString(str => str.Clear() .IsEqualTo("Is Equal To") .IsNotEqualTo("Is Not Equal To")) .ForNumber(str => str.Clear() .IsEqualTo("Is Equal To") .IsNotEqualTo("Is Not Equal To"))));

坚持之后