Laravel DB交易上下文可在称为功能上获得

发布于 2025-01-28 17:16:06 字数 515 浏览 4 评论 0原文

我发现,用于在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无人问我粥可暖 2025-02-04 17:16:06

请记住,您的代码可以使用不会返回重定向的任何其他功能/方法。这将跳过提交()或回滚()。一旦开始transAction()启动,它必须以commit()或滚动()结尾。

    DB::beginTransaction();
    try {
       /* your code */
       /* you can also use DB::commit(); at the end of TRY code like this */
    } catch (\Exception $ex) {
       DB::rollback();
       /* handle error */
       /* use return after rollback() */
    }
    DB::commit();

    /* use return after 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::beginTransaction();
    try {
       /* your code */
       /* you can also use DB::commit(); at the end of TRY code like this */
    } catch (\Exception $ex) {
       DB::rollback();
       /* handle error */
       /* use return after rollback() */
    }
    DB::commit();

    /* use return after commit(); */
梦年海沫深 2025-02-04 17:16:06

经过几次测试,我可以说,即使您在另一个功能内有一个创建方法,并且在主要或在功能中抛出了异常 - 回滚并提交脂肪都没有问题!
我希望更多!!!!

这是我测试的代码:

   DB::beginTransaction();
        try {
            $this->pippo();
        } catch (\Exception $ex) {
            DB::rollback();
        }
   DB::commit();

public function pippo(){
    $type=Cga_type::create(['name'=>'vvvv','description'=>'yyy']);
    throw new Exception('error');

}

如果我评论交易功能,则记录写在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:

   DB::beginTransaction();
        try {
            $this->pippo();
        } catch (\Exception $ex) {
            DB::rollback();
        }
   DB::commit();

public function pippo(){
    $type=Cga_type::create(['name'=>'vvvv','description'=>'yyy']);
    throw new Exception('error');

}

If I comment the transactions functions the record is written on the db, no otherwise!!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文