【问题标题】:Codeigniter - Transaction Test Mode Not WorkingCodeigniter - 事务测试模式不工作
【发布时间】:2011-09-15 22:20:04
【问题描述】:

那我做错了什么?当我运行以下代码时,即使事务处于测试模式,数据库也会始终更新。

/**
 * update_batch
 * This updates multiple rows. The data array must include the game_id and game_type_prize_id
 * @param array
 * @return bool
 * @author zechdc
 */
function update_batch($data)
{
    $result = TRUE;

    foreach($data as $prize)
    {
        $this->db->trans_start(TRUE); //first param is set to TRUE for test mode.
        $this->db->where('game_id', $prize['game_id']);
        $this->db->where('game_type_prize_id', $prize['game_type_prize_id']);
        $this->db->update('game_prizes', $prize);
        $this->db->trans_complete();

        if($this->db->affected_rows() == -1)
        {
            $result = FALSE;
        }
    }


    return $result;
}

【问题讨论】:

    标签: php codeigniter activerecord transactions


    【解决方案1】:

    AbhishekDilliwal 在他的 cmets 中为我们提供了答案。如果他发布答案,我将删除我的答案并接受他的答案,这样他就可以代替我获得荣誉。


    答案:

    Codeigniter 事务测试模式目前不起作用。


    解决方案:

    替换:

    $this->_trans_failure = ($test_mode === TRUE) ? TRUE : FALSE;
    

    与:

    $this->_trans_status = ($test_mode === TRUE) ? FALSE : $this->_trans_status;
    

    在以下codeigniter系统文件中:

    system/database/drivers/mysql/mysql_driver.php
    system/database/drivers/mysqli/mysqli_driver.php
    system/database/drivers/oci8/oci8_driver.php
    system/database/drivers/odbc/odbc_driver.php
    system/database/drivers/postgre/postgre_driver.php
    system/database/drivers/sqlite/sqlite_driver.php
    

    看起来rommelxcastro 已经将the fix 提交到了 github 存储库。现在我们只需要等待 Codeigniter 拉取它并发布一个新版本 :)


    来源:

    【讨论】:

    • 使用$this->_trans_status = ($test_mode === TRUE) ? FALSE : $this->_trans_status; 意味着如果将$test_mode 设置为TRUE,事务总是失败,这意味着它会回滚,这对于test_mode 来说是正确的,但是对于调试来说毫无用处。如果$test_mode 为真,我仍然希望我的交易“看起来”有效,如果它确实正确的话。
    【解决方案2】:

    您是否尝试过使用普通 SQL 编写查询,而不是使用 where() 和 update() 等库函数?我对此不确定,但 CI 文档仅在文档中显示 query() 函数。

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

    【讨论】:

    • 是的,这是一个已知问题,请在此处查看快速解决方案github.com/EllisLab/CodeIgniter/issues/5
    • @AbhishekDilliwal 请发表您的评论作为答案。我继续并将其发布为答案,但我想感谢您找到答案。所以请发布一个答案,我会删除我的。感谢您的帮助!
    猜你喜欢
    • 2013-05-05
    • 2015-11-03
    • 1970-01-01
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-24
    • 1970-01-01
    相关资源
    最近更新 更多