gpt4 book ai didi

Symfony2 : transactions fail with "There is no active transaction."

转载 作者:行者123 更新时间:2023-12-01 08:27:21 31 4
gpt4 key购买 nike

我花了几个小时试图解决这个问题。谷歌和 Stackoverflow 也没有多大帮助。所以这里非常欢迎任何建议。

我正在尝试在更新两个相关表时对事务应用回滚逻辑:

一般的代码是:

// ...
$em = $this->getDoctrine()->getEntityManager();
$em->getConnection()->beginTransaction();

foreach($dataArr as $data) {
$userObj = $em->getRepository('AcmeBundle:User')->find($userId);
$userObj->setActive(1);
$em->persist($userObj);
$em->getConnection()->commit();
}

$storeObj = $em->getRepository('AcmeBundle:Store')->find($storeId);
$storeObj->setIsOpen(1);
$em->persist($storeObj);
$em->getConnection()->commit();

try {

$em->flush();
$em->clear();

} catch(Exception $e) {

$em->getConnection()->rollback();
$em->close();
throw $e;

}

我的 PDO 驱动程序已启用,没有事务的更新按预期工作,但是一旦我 beginTransaction() 并尝试 commit() 没有任何效果,我得到了 没有活跃的交易。 异常(exception)。

一些消息来源建议只使用 commit() 而没有 persist() ,但这没有任何区别。我可能在这里做了一些非常愚蠢的事情,但我就是看不到它是什么。

最佳答案


$this->em->getConnection()->beginTransaction(); 

你必须写:
$this->em->getConnection()->setAutoCommit(false);

这个对我有用 :)

关于Symfony2 : transactions fail with "There is no active transaction.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30786765/

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