gpt4 book ai didi

transactions - Doctrine 1.2 : Using postDelete to delete related files on filesystem -- with transaction support

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

这个问题是这个问题的一种扩展:

Perform some cleanup when deleting a record in Symfony/Doctrine

我正在这样做并且它工作正常,除了一个问题:如果删除失败并且事务从未提交,则 postDelete 方法仍然运行并且文件无论如何都会被删除。

避免这种情况的好方法是什么?

最佳答案

如果你只需要在事务真正提交时触发某事,你需要在 postDelete 中排队,然后在 postTransactionCommit 处理程序中执行它

$conn = Doctrine_Manager::connection(...);
$conn->addListener(new TransactionCommitListener());

class TransactionCommitListener extends Doctrine_EventListener {

public function postTransactionCommit(Doctrine_Event $event) {
if ($event->getInvoker()->getConnection()->getTransactionLevel() == 1) {
// do your filesystem deletes here
}
}

public function postTransactionRollback(Doctrine_Event $event) {
// clear your delete queue here
}
}

我通常使用单例来存储队列并静态获取它。如果您使用多个连接,您将需要多个队列。

上面的提交处理程序只在最外面的提交上触发,这就是 Doctrine 与 mysql 一起工作的方式,因为它不提供嵌套事务。如果您的数据库提供嵌套事务,并且 Doctrine 支持,您可能需要更改它。

关于transactions - Doctrine 1.2 : Using postDelete to delete related files on filesystem -- with transaction support,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4069940/

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