Laravel DB交易上下文可在称为功能上获得
我发现,用于在Laravel中使用DB ::交易,与ORM结合使用,我们需要运行类似的内容:
DB::transaction(function() {
//
Model::create($something);
});
我的问题是: 如果我需要从封闭式内部调用其他功能,该功能运行其他创建方法,则“交易”环境中会有或在外面?
DB::transaction(function() {
//
ModelX::create($something);
$this->somefunction($data);
});
private function somefunction($data){
ModelY::create($data) <----this create is in the transaction or do I need a new one for having rollback in case of errros?
}
I have found that for using DB::transaction in Laravel, in combination with the ORM, we need to run something like:
DB::transaction(function() {
//
Model::create($something);
});
My question is:
If i need to call other function from inside the closure, that runs other creation method, there will be in the "transaction" enviroment or is it outside?
DB::transaction(function() {
//
ModelX::create($something);
$this->somefunction($data);
});
private function somefunction($data){
ModelY::create($data) <----this create is in the transaction or do I need a new one for having rollback in case of errros?
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请记住,您的代码可以使用不会返回重定向的任何其他功能/方法。这将跳过提交()或回滚()。一旦开始transAction()启动,它必须以commit()或滚动()结尾。
Remember that your code can use any additional functions/methods that will not return redirect. This will skip commit() or rollback(). Once the beginTransaction() started it has to end with commit() or rollback().
经过几次测试,我可以说,即使您在另一个功能内有一个创建方法,并且在主要或在功能中抛出了异常 - 回滚并提交脂肪都没有问题!
我希望更多!!!!
这是我测试的代码:
如果我评论交易功能,则记录写在DB上,没有其他否则!!!
after several test i can tell that no closure is needed for model too, even if you have a create method inside another function, and exception is throwed -in the main or in the function- no problem with the rollback and commit steatment!
Well much more of that i was hoping!!!!
This is the code I tested:
If I comment the transactions functions the record is written on the db, no otherwise!!!