gpt4 book ai didi

php - 即使计算的变更集为空,如何使 Doctrine 的 preUpdate 生命周期回调更新字段?

转载 作者:行者123 更新时间:2023-11-29 21:37:27 25 4
gpt4 key购买 nike

我有一个实体,希望在对其属性之一进行更改时更新其 updatedAt 字段(即使其中一个值更改为空字符串)。

因此,我有以下代码:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* MyEntity
*
* @ORM\Table(name="my_entity")
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*/
class MyEntity
{
// ...

/**
* @var datetime
*
* @ORM\Column(name="updated_at", type="datetime", nullable=false)
*/
protected $updatedAt;

// ...

/**
* @ORM\PreUpdate()
*/
public function preUpdate()
{
$this->updatedAt = new \DateTime();
}
}

所以当谈到:

$entity = $repository->findById($id);
$entity->setOneProperty('oneValue');
$entity->flush();

updatedAt 字段使用 MySQL 中的当前日期时间进行更新。

但是,当我需要将空字符串设置为值时,如下所示:

$entity->setOneProperty('');
$entity->flush();

当我需要时,updatedAt 字段没有更新。

我很确定这与 documentation 的以下部分有关:

preUpdate - The preUpdate event occurs before the database update operations to entity data. It is not called for a DQL UPDATE statement nor when the computed changeset is empty.

所以我的问题是如何覆盖此行为并让 Doctrine 更新字段,即使计算的变更集为空?我也愿意知道如何访问这个变更集,以便自己进行调查。

谢谢。

最佳答案

在将oneProperty设置为空字符串之前,其值是多少?

其中一种方法(但很难看)是在您需要的每个 setter 中更新您的属性 updatedAt (您可以尝试使用 __set)。它会起作用,但这不是一个好的解决方案。

关于php - 即使计算的变更集为空,如何使 Doctrine 的 preUpdate 生命周期回调更新字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34812120/

25 4 0
文章推荐: javascript - 捆绑 (jspm) React/jsx 应用程序时出现 ES6 错误
文章推荐: javascript - 使用 React 和一个
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com