Adding "INSERT IGNORE" to Active Record by Extending the CI Core Rather than Hacking the CI Core |
[eluser]Unknown[/eluser]
Initially, I used the method for adding INSERT IGNORE posted here: http://christiesmedia.co.uk/blog/3-blog/...ert-ignore But hacking the core files didn't sit well with me so I combined it with this method for extending database drivers: http://codeigniter.com/wiki/Extending_Database_Drivers/ I also had to make some small changes in MY_Loader.php to make it work with <b>CI 2.0</b>. To use it, place <b>MY_Loader.php</b> in <b>application/core/</b> and <b>MY_DB_mysql_driver.php</b> in <b>application/libraries/</b>. If you want to use it with <b>1.7.2</b> (not tested), you should copy the database function from the second tutorial above, exactly as is into MY_Loader.php, replacing my version of database(). You'll also have to place MY_Loader.php into application/libraries/ rather than application/core/. But again, I haven't tested it with 1.7.2 so please report back. Use it like this: $data = array( 'id' => 'ID' , 'title' => 'My title' , 'name' => 'My Name' , 'date' => 'My date' ); $this->db->ignore(); $this->db->insert('mytable', $data); // Produces: INSERT IGNORE INTO mytable (ID,title, name, date) VALUES ('ID','My title', 'My name', 'My date') Good luck! And thank you to the authors of the two tutorials!
[eluser]Gabriel Labrecque[/eluser]
Thanks a bunch, I was looking for this piece of code! One thing though, I saw that the article on christiesmedia.co.uk says to modify the _reset_write() method, not the _reset_select() one. The code you provided in application.zip overwrites the _reset_select() method. I guess the method should be replaced by the following: Code: function _reset_write() Also, one might need to IGNORE UPDATEs as well. I suggest adding the following to your code: Code: function _update($table, $values, $where, $orderby = array(), $limit = FALSE)
[eluser]Unknown[/eluser]
Thanks for the suggestions! Here's an updated version which includes UPDATE IGNORE. -Thom
[eluser]Unknown[/eluser]
This would be very useful for me tonight, but for some reason I cannot download the attachment... The forum just gives me a white screen, am I the only one?
[eluser]Freeze Dried Pop[/eluser]
Same here, could do with being able to download this.
[eluser]Freeze Dried Pop[/eluser]
Figured something out for myself in the end: First extend the CI_Loader by creating a MY_Loader.php file in the "core" folder containing the following: (courtesy of Simon Emms) Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); Then create a file called "MY_DB_mysql_driver.php" in the "core" folder and put the following: Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); I don't know if others will agree on it being the best way to do things, but it's the quickest and simplest. All it does is rather than run an insert, it just compiles it and then replaces the string "INSERT" with "INSERT INTO". |
Welcome Guest, Not a member yet? Register Sign In |