3

I am writing my first program with Codeigniter, and have run into a problem. I will start with a focused description of the problem and can broaden it if I need to:

I need to write a multi-dimensional array to the DB and want to use the insert_batch function from the CI_DB_active_record class to do so. The problem is that I need to write empty values as NULL for some fields while other fields need to be empty strings. The current function wraps all values with single quotes, and I cannot find a way to write null values to the database for specified fields.

I would also like to increase the number of records per batch.

I see how to extend models, libraries, etc., but is there a way to extend the CI_DB_active_record class without modifying core classes? The minimal amount of core class modification to make this work that I have found is modifying the following lines in the DB.php file (changing the require_once file to the new file that extends the CI_DB_active_record class and changing the CI_DB_active_record class name to the new class name):

require_once(BASEPATH.'database/DB_active_rec'.EXT);

        if ( ! class_exists('CI_DB'))
        {
            eval('class CI_DB extends CI_DB_active_record { }');
        }

Can I do better?

ctrane
  • 61
  • 1
  • 7

1 Answers1

2

There is no out-of-the-box solution to do this. Your hack is a reasonable solution and if it does the job then spot on.

If you have any changes to make to the core (plenty of room for improvement in some parts of AR) then why not hop on BitBucket and help out with Reactor?

https://bitbucket.org/ellislab/codeigniter-reactor

Phil Sturgeon
  • 30,637
  • 12
  • 78
  • 117
  • disagreed to answer your question : but is there a way to extend the CI_DB_active_record class without modifying core classes? yes there is a way to extend core classes. http://codeigniter.com/user_guide/general/core_classes.html – bonez Mar 16 '11 at 22:08
  • Of course you can extend core libraries, but the database class is not a normal library and is not treated as one, therefore the Loader library has no knowledge of your MY_DB_active_record. Look at the link you have just posted, do you see any reference of the Database class on there? – Phil Sturgeon Mar 17 '11 at 11:37
  • bonez - I did try your solution originally, but as Phil said, the active record class is not extendable like the other core classes. – ctrane Mar 21 '11 at 14:50
  • Thanks for you input Phil, and for all you do for CI. – ctrane Mar 21 '11 at 14:53