Replace all of that code with:
$this->db->query("
INSERT INTO event_entry (event_id, client_id)
SELECT ?, client_id
FROM event
", array($event_id));
And you will clearly notice the difference in execution time :) Plus in my opinion, having less code to worry about.
This query can't be run from Active Records, but it should be quite self explaining. Just like a normal SELECT
, if fetches client_id
and the already defined value $event_id
by each event
row. It then takes these values, and INSERT
them into event_entry
.
Note that ?
and array($event_id)
insert the value into the query escaped (and safe). Never insert into query as SELECT {$event_id}, client_id
unless you know what you're doing.
Jeemusu's solution is indeed a nice way to do it through Active Records, but if it's performance you want all the way, one query is faster than two in this case.