gpt4 book ai didi

symfony - Gedmo\Loggable 记录未更改的数据

转载 作者:行者123 更新时间:2023-12-04 07:24:24 24 4
gpt4 key购买 nike

我将 Symfony2.2 与 StofDoctrineExtensionsBundle(以及 Gedmo DoctrineExtensions)一起使用。
我有一个简单的实体

/**
* @ORM\Entity
* @Gedmo\Loggable
* @ORM\Table(name="person")
*/
class Person {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;

[...]

/**
* @ORM\Column(type="datetime", nullable=true)
* @Assert\NotBlank()
* @Assert\Date()
* @Gedmo\Versioned
*/
protected $birthdate;
}

更改现有对象的属性时,会在表 ext_log_entries 中完成日志条目。此日志表中的条目仅包含更改的列。我可以通过以下方式阅读日志:
$em = $this->getManager();
$repo = $em->getRepository('Gedmo\Loggable\Entity\LogEntry');
$person_repo = $em->getRepository('Acme\MainBundle\Entity\Person');

$person = $person_repo->find(1);
$log = $repo->findBy(array('objectId' => $person->getId()));
foreach ($log as $log_entry) { var_dump($log_entry->getData()); }

但我不明白的是,为什么 birthdate 字段总是包含在日志条目中,即使它没有改变。以下是三个日志条目的一些示例:
array(9) {
["salutation"]=>
string(4) "Herr"
["firstname"]=>
string(3) "Max"
["lastname"]=>
string(6) "Muster"
["street"]=>
string(14) "Musterstraße 1"
["zipcode"]=>
string(5) "00000"
["city"]=>
string(12) "Musterhausen"
["birthdate"]=>
object(DateTime)#655 (3) {
["date"]=>
string(19) "1893-01-01 00:00:00"
["timezone_type"]=>
int(3)
["timezone"]=>
string(13) "Europe/Berlin"
}
["email"]=>
string(17) "email@example.com"
["phone"]=>
NULL
}

array(2) {
["birthdate"]=>
object(DateTime)#659 (3) {
["date"]=>
string(19) "1893-01-01 00:00:00"
["timezone_type"]=>
int(3)
["timezone"]=>
string(13) "Europe/Berlin"
}
["phone"]=>
string(9) "123456789"
}

array(2) {
["birthdate"]=>
object(DateTime)#662 (3) {
["date"]=>
string(19) "1893-01-01 00:00:00"
["timezone_type"]=>
int(3)
["timezone"]=>
string(13) "Europe/Berlin"
}
["phone"]=>
NULL
}

我只想记录真正改变的数据。有没有我还没有看到的选项?这似乎与 birthdate 是一个 DateTime 对象的事实有关,不是吗?

编辑
它与 DateTime 对象无关。即使在其他实体中也会发生这种情况。我有另一个包含简单值的实体:
/**
* @ORM\Entity
* @Gedmo\Loggable
* @ORM\Entity(repositoryClass="Acme\MainBundle\Repository\ApplicationRepository")
* @ORM\Table(name="application")
*/
class Application {

[...]

/**
* @ORM\Column(type="integer")
* @Assert\NotBlank(groups={"FormStepOne", "UserEditApplication"})
* @Gedmo\Versioned
*/
protected $insurance_number;
}

当在浏览器中打开编辑表单并保存而不修改时,日志表包含:
update  2013-04-26 11:32:42     Acme\MainBundle\Entity\Application  a:1:{s:16:"insurance_number";s:7:"1234567";}
update 2013-04-26 11:33:17 Acme\MainBundle\Entity\Application a:1:{s:16:"insurance_number";s:7:"1234567";}

为什么?

最佳答案

这可能与我在使用这些扩展中的另一个(时间戳)时遇到的问题类似,即:学说中使用的默认更改跟踪策略(它尝试自动检测更改)有时将实体标记为脏,而当它们不是(对我来说,这是在我的实体包含一个日期时间对象时发生的,这是可以理解的,因为这是一个从数据库中提取时需要构造的对象)。这不是错误或任何东西 - 这是预期的行为,并且有几种解决方法。

可能值得尝试在要记录的实体上实现替代更改跟踪策略并查看是否可以解决问题 - 我猜除非实体状态是脏的,否则这种行为(日志记录)不会启动,您可以避免这种情况通过手动实现变更跟踪:

http://docs.doctrine-project.org/en/latest/cookbook/implementing-the-notify-changetracking-policy.html

不要忘记更新您的实体:

YourBundle\Entity\YourThing:
type: entity
table: some_table
changeTrackingPolicy: NOTIFY

看到这个线程:

https://github.com/Atlantic18/DoctrineExtensions/issues/333#issuecomment-16738878

关于symfony - Gedmo\Loggable 记录未更改的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16233093/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com