Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 24 revisions

Category:Libraries Category:Libraries::Database

(Originally discussed at [url]http://www.codeigniter.com/forums/viewthread/612/[/url])

Here's an [url=http://adodb.sourceforge.net/]ADOdb[/url] library for use in CI.

What are the benefits of Adodb? It's mature, and supports [url=http://phplens.com/adodb/supported.databases.html]lots of databases[/url]. I really like some of the features in it, [url=http://phplens.com/lens/adodb/docs-adodb.htm#autoexecute]autoexecute()[/url], and [url=http://phplens.com/lens/adodb/docs-adodb.htm#getrow]getrow()[/url] are particularily useful.

  1. Grab ADODB from [url=http://sourceforge.net/project/showfiles.php?group_id=42718]http://sourceforge.net/project/showfiles.php?group_id=42718[/url]
  2. Unzip to system/application/libraries/adodb/
  3. Optionally apply ConfigSectionsPatch (as of CI 1.3.3, this is not included), otherwise you'll have to create a valid config/adodb.php or you'll get a CI error.
  4. Add the following to system/application/init/init_adodb.php:

[code] <?php if (!defined('BASEPATH')) exit('No direct script access allowed');

if (!function_exists('_init_adodb_library')) { function _init_adodb_library(&$ci) { $db_var = false; $debug = false;

    // try to load config/adodb.php
    if ($ci->config->load('adodb',true)) {
        $cfg = $ci->config->item('adodb');
        if (isset($cfg['dsn'])) {
            $dsn = $cfg['dsn'];
        }
        
        // set db_var if it's set in the config file, or false otherwise
        $db_var = isset($cfg['db_var']) && $cfg['db_var'];
        
        $debug = isset($cfg['debug']) && $cfg['debug'];
    } 
    
    if (!isset($dsn)) {
        // fallback to using the CI database file
        include(APPPATH.'config/database'.EXT);
        $group = 'default';
        $dsn = $db[$group]['dbdriver'].'://'.$db[$group]['username']
               .':'.$db[$group]['password'].'@'.$db[$group]['hostname']
               .'/'.$db[$group]['database'];
    }
    
    // $ci is by reference, refers back to global instance
    $ci->adodb =& ADONewConnection($dsn);
    
    if ($db_var) {
        // also set the normal CI db variable
        $ci->db =& $ci->adodb;
    }
    
    if ($debug) {
        $ci->adodb->debug = true;
    }
}

}

if ( ! class_exists('ADONewConnection') ) { require_once(APPPATH.'libraries/adodb/adodb.inc'.EXT); }

$obj =& get_instance(); _init_adodb_library($obj); $obj->ci_is_loaded[] = 'adodb';

?> [/code]

Note that you need the patch at http://www.codeigniter.com/forums/viewthread/611/ for the init to work, or alternatively, you can create a config/adodb.php file, or comment out the whole IF block where it mentions that patch url.

By default, it will create an "adodb" object ($this->adodb) that is connected to the database defined in application/config/database.php (currently hardcoded to use the 'default' group). Alternatively, you can create an application/config/adodb.php file:

[code]<?php $config['adodb']['dsn'] = 'mysql://user:pass@localhost/dbname'; $config['adodb']['db_var'] = true; $config['adodb']['debug'] = false; ?>[/code]

All values are optional.

[b]dsn[/b] can be any valid DSN string, as described at http://phplens.com/lens/adodb/docs-adodb.htm#connect_ex. If not specified, the settings in config/database.php are used. [b]db_var[/b] if true will set $this->db to be a reference to $this->adodb, overwriting CodeIgniter's database object. Defaults to false. [b]debug[/b] sets ADOdb debug mode, defaults to false.

You can get going very fast: [code] $this->load->library('adodb'); $row = $this->adodb->getrow('SELECT * FROM users WHERE user_id = 1'); echo 'hello '.$row['firstname']; [/code]

You can also of course auto-load this like any other library, by adding it to the 'core' array in application/config/autoload.php:

[code]$autoload['core'] = array('adodb');[/code]

In the future..

Something that may be useful is to write wrappers for the ADOdb object that emulate all the functions in the CI database class, as well as the QueryResult class. Now, this adds abstraction to an abstraction layer.. so it may be unnecessarily complex, not to mention you kind of defeat the purpose of using ADOdb.

I have not yet played with the ADOdb [url=http://phplens.com/lens/adodb/docs-active-record.htm]active record[/url] stuff, but it may be possible to integrate it with models somehow.

Clone this wiki locally