【问题标题】:Doctrine - Event Listener (preUpdate) not updating related entitiesDoctrine - 事件监听器(preUpdate)不更新相关实体
【发布时间】:2019-03-13 13:56:56
【问题描述】:

我有一个实体客户端和一个链接实体电子邮件。

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 或其他)检查只有一封默认 = 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 保存电子邮件。但是这些电子邮件的 db 没有变化。怎么了?

【问题讨论】:

    标签: php doctrine event-listener flush persist


    【解决方案1】:

    你不见了

    $entityManager->flush();

    坚持之后

    【讨论】:

    • 我在输入 flush() 时出现此错误:致命错误:未捕获错误:已达到“256”的最大函数嵌套级别,正在中止!在
    • 好吧,只是一个放错位置的 flush()。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    • 1970-01-01
    相关资源
    最近更新 更多