我在 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;
事实是,如果我不让它成为一个事务(没有 $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 (!$this->ajout_ressource($ressourceCsv)) { //active record insertion return true
$failure[] = $ressourceCsv['title']; // if successful
}
}
$this->db->trans_complete();
return $failure;