2

Is there a way to grab all the insert ids ( using $this->db->insert_id() ) from a previously run insert_batch() method?

Ideally it will spit out a simple array of the ids in the order they were inserted.

aowie1
  • 836
  • 8
  • 21
  • 1
    Not for codeigniter,but it will help you some amount..Please go through http://stackoverflow.com/questions/1285231/php-mysql-retrieving-the-last-inserted-ids-for-multiple-rows – Duke Mar 07 '12 at 06:43
  • Thanks duke, guess I just assumed that the insert_id output the LAST inserted row, not the first. This is good information and I will be using this method instead of trying to parse into an array. – aowie1 Mar 07 '12 at 19:00

1 Answers1

4

creating a trigger would work.

not sure if its gona affect performance on huge batch insertion

DELIMITER $$

CREATE
    TRIGGER `test`.`getids` AFTER INSERT
    ON `database_name`.`table_name`
    FOR EACH ROW BEGIN
        INSERT INTO last_inserted_ids (last_insertId) VALUES(LAST_INSERT_ID());

    END$$

DELIMITER ;

it will get all the ids into the table, as you want them in an array write a query which run exactly after the batch and gets all the values from table last_inserted_ids and then truncate it so that you always have the desired ids after a batch is executed.

hope this helps your case.

Junaid
  • 2,084
  • 1
  • 20
  • 30
  • This is a pretty cool concept, and I will mark as accepted answer because it does what I asked for, allowing me to parse into array. But for performance purposes, and the fact that I'm not doing anything funky with auto incrementing, I will go with dukes comment. Thanks! – aowie1 Mar 07 '12 at 18:58