Codeigniter, error tracking in transaction Codeigniter, error tracking in transaction codeigniter codeigniter

Codeigniter, error tracking in transaction


I believe that once an error occurs inside a transaction, you must rollback before any more DB mods can be made. That would explain the behavior you are seeing. After the first error, the transaction is "aborted" and you continue your loop, causing every subsequent SQL command to fail as well. This can be illustrated as follows:

db=# select * from test1; id | foo | bar----+-----+-----(0 rows)db=# begin;BEGINdb=# insert into test1 (foo, bar) values (1, 'One');INSERT 0 1db=# insert into test1 (foo, bar) values (Oops);ERROR:  column "oops" does not existLINE 1: insert into test1 (foo, bar) values (Oops);                                             ^db=# insert into test1 (foo, bar) values (2, 'Two');ERROR:  current transaction is aborted, commands ignored until end of transaction blockdb=# select * from test1;ERROR:  current transaction is aborted, commands ignored until end of transaction blockdb=# commit;ROLLBACKace_db=# select * from test1; id | foo | bar----+-----+-----(0 rows)db=#

Note it seems that "commit" does a "rollback" if there was an error (it was not a typo.)

Also BTW: use $this->db->trans_status() === FALSE to check for an error during the transaction.

Update: Here's some (untested) code to do it in a transaction so that the inserts are not seen by others until you are ready:

$failure = array(); //the array where we store what failed$done = false;do {    $this->db->trans_begin();    foreach ($data as $key => $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->trans_status() === false) { // an insert failed            $failure[] = $ressourceCsv['title'];   // save the failed title            unset($data[$key]);                    // remove failed insert from data set            $this->db->trans_rollback();           // rollback the transaction            break;                                 // retry the insertion        }    }    $done = true;                                  // completed without failure} while (count($data) and ! $done);                // keep going until no data or success/* * Two options (uncomment one): * 1. Commit the successful inserts even if there were failures.$this->db->trans_commit(); * 2. Commit the successful inserts only if no failures.if (count($failure)) {    $this->db->trans_rollback();} else {    $this->db->trans_commit();}*/return $failure;