Laravel DB::transaction 异常 PDO 没有活动事务
Laravel DB::transaction exception PDO There is no active transaction
我有以下代码:
$new_models = DB::transaction(function () use ($supplier, $address, $addressDetail) {
$new_supplier = $this->setNewSupplier($supplier);
$new_address = $this->setNewAddress($address);
$new_addressDetail = $this->setNewAddressDetail($addressDetail,$new_address->id);
$this->syncSupplierAddress($new_supplier->id,$new_address->id);
$this->updateControlAp($new_supplier->supplier_id);
return [$new_supplier, $new_address, $new_addressDetail];
});
set 方法基本上是在最后用 save() 创建模型对象;
现在,如果 2nd...nth 失败但如果第一个失败则不会。
如果$this->setNewSupplier($supplier);
比我得到的失败
"PDOException in Connection.php line 541:
There is no active transaction"
我是不是做错了什么?此外,如果我从供应商 Connection.php 中的 catch 中评论 $this->rollBack();
,它实际上会给我 SQL 错误。这里的重要部分是,只有当第一次 save() 失败时这才有效
PS。我使用的是 PostgreSQL 而不是 MySQL 但我不认为它相关
在Laravel中有不同的交易方式,另一种可能是:
...
$new_models = [];
try {
DB::beginTransaction();
$new_supplier = $this->setNewSupplier($supplier);
$new_address = $this->setNewAddress($address);
$new_addressDetail = $this->setNewAddressDetail($addressDetail,$new_address->id);
$this->syncSupplierAddress($new_supplier->id,$new_address->id);
$this->updateControlAp($new_supplier->supplier_id);
$new_models = [$new_supplier, $new_address, $new_addressDetail];
DB::commit();
} catch(\Exception $e) {
DB::rollback();
// Handle Error
}
...
我有以下代码:
$new_models = DB::transaction(function () use ($supplier, $address, $addressDetail) {
$new_supplier = $this->setNewSupplier($supplier);
$new_address = $this->setNewAddress($address);
$new_addressDetail = $this->setNewAddressDetail($addressDetail,$new_address->id);
$this->syncSupplierAddress($new_supplier->id,$new_address->id);
$this->updateControlAp($new_supplier->supplier_id);
return [$new_supplier, $new_address, $new_addressDetail];
});
set 方法基本上是在最后用 save() 创建模型对象;
现在,如果 2nd...nth 失败但如果第一个失败则不会。
如果$this->setNewSupplier($supplier);
比我得到的失败
"PDOException in Connection.php line 541:
There is no active transaction"
我是不是做错了什么?此外,如果我从供应商 Connection.php 中的 catch 中评论 $this->rollBack();
,它实际上会给我 SQL 错误。这里的重要部分是,只有当第一次 save() 失败时这才有效
PS。我使用的是 PostgreSQL 而不是 MySQL 但我不认为它相关
在Laravel中有不同的交易方式,另一种可能是:
...
$new_models = [];
try {
DB::beginTransaction();
$new_supplier = $this->setNewSupplier($supplier);
$new_address = $this->setNewAddress($address);
$new_addressDetail = $this->setNewAddressDetail($addressDetail,$new_address->id);
$this->syncSupplierAddress($new_supplier->id,$new_address->id);
$this->updateControlAp($new_supplier->supplier_id);
$new_models = [$new_supplier, $new_address, $new_addressDetail];
DB::commit();
} catch(\Exception $e) {
DB::rollback();
// Handle Error
}
...