使用 Datamapper/Codeigniter 提交事务后可以回滚事务吗?

发布于 2024-12-01 15:16:12 字数 390 浏览 0 评论 0原文

事务提交后是否可以回滚? 我问这个问题是因为在 Datamapper 文档中我看到了 trans_begin() 方法,但我没有找到 trans_end() 方法。 Codeigniter 有 trans_complete() 方法,所以我假设 Datamapper 可能有类似的方法。

我发现有趣的一件事是 这个答案。 Datamapper/Codeigniter 中是否有类似于保存点的东西?

Is it possible to rollback a transaction after it has been commited?
I ask this because in the Datamapper documentation i see the trans_begin() method, but i didn't find the trans_end() method. Codeigniter has the trans_complete() method, so i assumed Datamapper might have a similar method.

One thing I found interesting is this answer. Is there anything similar to a savepoint in Datamapper/Codeigniter?

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

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

发布评论

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

评论(1

人海汹涌 2024-12-08 15:16:12

http://datamapper.wanwizard.eu/pages/transactions.html< /p>

DataMapper 处理事务的方式与
CodeIgniter 确实如此(请阅读 CodeIgniter Transactions),显然是因为它
使用相同的方法!唯一真正的区别是你会
直接在 DataMapper 对象上调用事务方法。

这意味着,

$this->db->trans_begin();

您将使用:

$my_datamapper_object->trans_begin();

根据 Datamapper 文档,其他所有内容在事务方面与 Codeigniter 相同。如果您查看 Datamapper 库的源代码,您会发现所有 trans_*() 调用都只是包装函数。示例:

// Datamapper.php 1.8.dev line 3975
/**
 * Trans Complete
 *
 * Complete a transaction.
 *
 * @return  bool Success or Failure
 */
public function trans_complete()
{
    return $this->db->trans_complete();
}

Codeigniter 或 Datamapper 中都不存在 trans_end() 方法。您可以使用 trans_start()trans_complete() 进行自动事务,或手动调用它们:

http://codeigniter.com/user_guide/database/transactions.html< /p>

手动运行事务

如果您想手动运行交易,您可以这样做
如下:

$this->db->trans_begin();

$this->db->query('AN SQL QUERY...');
$this->db->query('另一个查询...');
$this->db->query('还有另一个查询...');

if ($this->db->trans_status() === FALSE)
{
    $this->db->trans_rollback();
}
别的
{
    $this->db->trans_commit();
}

只需将 $this->db 替换为您的 Datamapper 对象即可。例如。

$u = new User($id);
$u->trans_begin();
$u->name = 'Jorge';
$u->save();

if ($u->trans_status() === FALSE)
{
    $u->trans_rollback();
}
else
{
    $u->trans_commit();
}

http://datamapper.wanwizard.eu/pages/transactions.html

DataMapper handles transactions in very much the same way that
CodeIgniter does (read CodeIgniter Transactions), obviously because it
uses the same methods! The only real difference is that you'll be
calling the transaction methods directly on your DataMapper objects.

This means that instead of:

$this->db->trans_begin();

You'd use:

$my_datamapper_object->trans_begin();

Everything else, according to the Datamapper documentation, is identical to Codeigniter in regards to transactions. If you look at the source code for the Datamapper library, you'll see that all trans_*() calls are simply wrapper functions. Example:

// Datamapper.php 1.8.dev line 3975
/**
 * Trans Complete
 *
 * Complete a transaction.
 *
 * @return  bool Success or Failure
 */
public function trans_complete()
{
    return $this->db->trans_complete();
}

A trans_end() method does not exist in either Codeigniter or Datamapper. You'd use trans_start() and trans_complete() for automatic transactions, or to call them manually:

http://codeigniter.com/user_guide/database/transactions.html

Running Transactions Manually

If you would like to run transactions manually you can do so as
follows:

$this->db->trans_begin();

$this->db->query('AN SQL QUERY...');
$this->db->query('ANOTHER QUERY...');
$this->db->query('AND YET ANOTHER QUERY...');

if ($this->db->trans_status() === FALSE)
{
    $this->db->trans_rollback();
}
else
{
    $this->db->trans_commit();
}

Just replace $this->db with your Datamapper object. For example.

$u = new User($id);
$u->trans_begin();
$u->name = 'Jorge';
$u->save();

if ($u->trans_status() === FALSE)
{
    $u->trans_rollback();
}
else
{
    $u->trans_commit();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文