Duplicating records in CodeIgniter is kind of easy thanks to the framework’s Query Builder Class.
In your model, create a function that read record based on id
then clone it depends on the $count
parameter.
function duplicate($id, $count) { $this->db->where('id', $id); $query = $this->db->get($table); foreach ($query->result() as $row){ foreach($row as $key => $val){ if($key != 'id'){ $this->db->set($key, $val); } } } $result = array(); for ($i=0; $i < $count; $i++) { $result[] = $this->db->insert($table); } return $result; }
$table
should be the name of the model’s table .
Then you can use it in any controllers.
//duplicate the record with id 5 10 times $this->my_model->duplicate(5, 10);