aowie1
aowie1

Reputation: 846

Codeigniter: Retrieve insert_ids using insert_batch()

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.

Upvotes: 2

Views: 4193

Answers (1)

Junaid
Junaid

Reputation: 2094

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.

Upvotes: 4

Related Questions