CodeIgniter Forums

Full Version: Caching in custom database group.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

I have following case:
database config group called 'configuration' and of course an 'default' group.
I'm trying to load database 'configuration' without loading 'default' database in library which is loaded in controller constructor.
so my library constructor looks like this:
function __construct() {
    $this->obj =& get_instance();
    $this->db = $this->obj->load->database('configuration', TRUE);

my part of configuration group looks like:
$db['configuration']['cache_on'] = TRUE;
$db['configuration']['cachedir'] = "/tmp";

and when I trying load my library I have following error:
Quote:Fatal error: Call to a member function cache_off() on a non-object in ...system/database/DB_cache.php on line 58

Obviously '/tmp' dir has access for httpd process and it doesn't matter if I use 'mysql' or 'mysqli' driver.

If I place in constructor before loading 'configuration' group following code:
Error is gone - but I'm not sure if then I have cached 'configuration' group instead of only group 'default'.

El Forum

I think cache will only work with the database object assigned to $CI->db. I don't find any note about this in the documentation, and I think this can be easily corrected...
On the _cache_init method the Cache object is instantiated (line 1086)... you can pass the database instance to the constructor and replacing the $this->CI->db call for $this->db... something like this

database/DB_driver.php - line 1086
$this->CACHE = new CI_DB_Cache;
//replaced by
$this->CACHE = new CI_DB_Cache($this);

class CI_DB_Cache {

    var $CI;
    var $db;

     * Constructor
     * Grabs the CI super object instance so we can access it.
    function CI_DB_Cache(&$db)
        // Assign the main CI object to $this->CI
        // and load the file helper since we use it a lot
        $this->CI =& get_instance();
        $this->db =& $db;

    // --------------------------------------------------------------------
and then, on the same file, search & replace all $this->CI->db for $this->db

I've tested it here and it seems to work =)

El Forum

Thank you!

I think this should be included in next CI realease.