5
我正在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;
的事实是,如果我不让它交易(无$这个 - > DB->反式_...),它完美的作品,我有一个包含几个数组标题。但是对于事务,数组包含自第一个错误以来的所有标题。有没有办法只从插入导致事务回滚的标题?
我也曾尝试用:
$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;
好了,如果我是正确的,在交易的非常结构不允许我按照我的意愿进行。另外我不能使用$ this-> db-> trans_status()=== FALSE,因为我实际上将事务行包装在if语句中(我并不总是使用事务)。为了解决我的问题,我终于存储了成功插入的ID,最后删除它们,如果我想要回滚并且$ failure数组不是空的 – Dargor
我想如果你必须**一次完成所有的插入操作任何失败,那么你可以做到这一点,但如果你的意图是没有人会看到任何插入,如果一些失败,那么你不能这样做。你可以有一个名为“accepted”的布尔列,并且只有在所有插入完成并且没有失败之后才将其设置为TRUE,并向其他查询添加一个WHERE accepted = TRUE'子句。 – user9645
我添加了更新以显示如何在事务内执行循环(如果需要)。 – user9645