23 2 / 2012

Codeigniter on_duplicate_update db function

So your using MYSQL for your new codeigniter app and you get to a point in the app where you need to do this.

So as show above, you check if a record exists, if it does you update, if not you insert. This is fast and easy correct? But what if you doing thousand of records and you need some help in performance. Well in Mysqli you have the ability to insert on duplicate all in one query.

Ok thats great, but how can we make this amazingly awesome in codeigniter?? Well its a pain in the ass. But its possible.

$db[‘default’][‘dbdriver’] = ‘mysqli’;

First since codeigniter doesnt allow up to extend the active record class you have to add this section to replace the exisiting section in /database.DB.php

Then once this is done, inside of your application core, youll need to add 3 files.

MY_DB_active_rec.php

MY_DB_mysqli_driver.php

and finally MY_Loader.php

And then you can use this like this

Enjoy. :)

Permalink 2 notes