小编Dar*_*gor的帖子

Codeigniter,事务中的错误跟踪

我在CodeIgniter中运行一个小方法来在数据库中插入一些行(同一个表).我想看看在事务中哪个插入失败(通过返回标题数组).我的代码是:

$failure = array(); //the array where we store what failed
$this->db->trans_start();
foreach ($data as $ressourceCsv){ //data is an array of arrays to feed the database
    $this->ajout_ressource($ressourceCsv); //method to insert (basically, just an insert with active record)
    if (($this->db->_error_message())!=null) { 
          $failure[] = $ressourceCsv['title']; 
    }
}
$this->db->trans_complete();
return $failure;
Run Code Online (Sandbox Code Playgroud)

事实是,如果我不使它成为一个事务(没有$ this-> db-> trans _...),它完美地工作,我有一个包含几个标题的数组.但是对于事务,数组包含自第一个错误以来的所有标题.有没有办法从插入中获取导致事务回滚的标题?

我也尝试过:

$failure = array(); //the array where we store what failed
$this->db->trans_start();
foreach ($data as $ressourceCsv){ //data is an array of arrays to feed the database

    if …
Run Code Online (Sandbox Code Playgroud)

database activerecord transactions codeigniter insertion

5
推荐指数
1
解决办法
3924
查看次数