Codeigniter 中具有多个表的事务

发布于 2024-08-29 07:43:24 字数 1871 浏览 1 评论 0原文

总的来说,我对事务很陌生,尤其是 CodeIgniter。我正在使用 InnoDB 和其他所有东西,但我的事务在我想要的时候并没有回滚。这是我的代码(稍微简化)。

            $dog_db = $this->load->database('dog', true);
            $dog_db->trans_begin();

            $dog_id = $this->dogs->insert($new_dog); //Gets primary key of insert
            if(!$dog_id)
            {
                $dog_db->trans_rollback();
                throw new Exception('We have had an error trying to add this dog.  Please go back and try again.');
            }

            $new_review['dog_id'] = $dog_id;
            $new_review['user_id'] = $user_id;
            $new_review['date_added'] = time();

            if(!$this->reviews->insert($new_review)) //If the insert fails
            {
                $dog_db->trans_rollback();
                throw new Exception('We have had an error trying to add this dog.  Please go back and try again.');
            }

                //ADD DESCRIPTION
            $new_description['description'] = $add_dog['description'];
            $new_description['dog_id'] = $dog_id;
            $new_description['user_id'] = $user_id;
            $new_description['date_added'] = time();

            if(!$this->descriptions->insert($new_description))
            {
                $dog_db->trans_rollback();
                throw new Exception('We have had an error trying to add this dog.  Please go back and try again.');
            }

                $dog_db->trans_rollback();  //THIS IS JUST TO SEE IF IT WORKS
                throw new Exception('We have had an error trying to add this dog.  Please go back and try again.');

            $dog_db->trans_commit();
}

catch(Exception $e)
{
    echo $e->getMessage();
}

我没有收到任何错误消息,但也没有回滚。它应该在提交之前的最后一个 trans_rollback 处回滚。我的模型都在“狗”数据库上,所以我认为事务将带入模型的功能中。也许你只是不能使用这样的模型。任何帮助将不胜感激!谢谢!

I'm new to transactions in general, but especially with CodeIgniter. I'm using InnoDB and everything, but my transactions aren't rolling back when I want them to. Here's my code (slightly simplified).

            $dog_db = $this->load->database('dog', true);
            $dog_db->trans_begin();

            $dog_id = $this->dogs->insert($new_dog); //Gets primary key of insert
            if(!$dog_id)
            {
                $dog_db->trans_rollback();
                throw new Exception('We have had an error trying to add this dog.  Please go back and try again.');
            }

            $new_review['dog_id'] = $dog_id;
            $new_review['user_id'] = $user_id;
            $new_review['date_added'] = time();

            if(!$this->reviews->insert($new_review)) //If the insert fails
            {
                $dog_db->trans_rollback();
                throw new Exception('We have had an error trying to add this dog.  Please go back and try again.');
            }

                //ADD DESCRIPTION
            $new_description['description'] = $add_dog['description'];
            $new_description['dog_id'] = $dog_id;
            $new_description['user_id'] = $user_id;
            $new_description['date_added'] = time();

            if(!$this->descriptions->insert($new_description))
            {
                $dog_db->trans_rollback();
                throw new Exception('We have had an error trying to add this dog.  Please go back and try again.');
            }

                $dog_db->trans_rollback();  //THIS IS JUST TO SEE IF IT WORKS
                throw new Exception('We have had an error trying to add this dog.  Please go back and try again.');

            $dog_db->trans_commit();
}

catch(Exception $e)
{
    echo $e->getMessage();
}

I'm not getting any error messages, but it's not rolling back either. It should roll back at that final trans_rollback right before the commit. My models are all on the "dog" database, so I think that the transaction would carry into the models' functions. Maybe you just can't use models like this. Any help would be greatly appreciated! Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

染火枫林 2024-09-05 07:43:24

好吧,我知道这篇文章已经过时了,但这是我的 2 美分:

我不认为这个:

if(!$this->descriptions->insert($new_description))

会起作用,因为 CI 活动记录的插入函数总是返回 TRUE(成功与否)。如果您使用调试模式,CI 将在错误时停止并向用户抛出一条屏幕消息,但插入函数仍将返回 TRUE。

因此,如果您愿意使用 CI“手动”控制事务,则必须使用如下内容:

...

$this->db->trans_begin();

$this->db->insert('FOO');

if ($this->db->trans_status() === FALSE){

    $this->db->trans_rollback();

}else{

    $this->db->trans_commit();

}

希望这可以帮助某人...有时...某地

Well, I know this post is antique, but here's my 2 cents:

I don't think this:

if(!$this->descriptions->insert($new_description))

will work, cause the insert function from CI active record always returns TRUE (succeeding or not). If you are using debug mode, CI will stop on error and throw a screen message to the user, but insert function will still returns TRUE.

So, if you're willing to control transactions "manualy" with CI, you'll have to use something like this:

...

$this->db->trans_begin();

$this->db->insert('FOO');

if ($this->db->trans_status() === FALSE){

    $this->db->trans_rollback();

}else{

    $this->db->trans_commit();

}

Hope this helps someone...sometime....somewhere

划一舟意中人 2024-09-05 07:43:24

也许,这是因为您使用 $dog_db 连接,并回滚不存在的 $booze_db 事务?(或者这是一个拼写错误?)

Maybe, it's because you connected using $dog_db, and rolling back nonexisting $booze_db transaction ?(or it's a typo?)

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