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.

2
  • 1
    Not for codeigniter,but it will help you some amount..Please go through stackoverflow.com/questions/1285231/… Commented Mar 7, 2012 at 6: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. Commented Mar 7, 2012 at 19:00

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.