Skip to content
Derek Jones edited this page Jul 5, 2012 · 24 revisions

Introduction

(Originally discussed here) Here's an ADOdb library for use in CI.

What are the benefits of Adodb? It's mature, and supports lots of databases. I really like some of the features in it, autoexecute(), and getrow() are particularly useful.

Setup

  1. Grab ADODB
  2. Unzip to system/application/libraries/adodb/
  3. Optionally apply ConfigLoadPatch (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 (if you apply the patch, ADOdb will be able to detect that config/adodb.php doesn't exist and instead read your config/database.php file).
  4. Add the following source code to the appropriate directory

Source

system/application/init/init_adodb.php

<?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
        // extra parameter comes from patch at http://www.codeigniter.com/wiki/ConfigLoadPatch/
        // without this patch, if config/adodb.php doesn't exist, CI will display a fatal error.
        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';

?>

Usage

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:

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

All values are optional.

dsn 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. db_var if true will set $this->db to be a reference to $this->adodb, overwriting CodeIgniter's database object. Defaults to false. debug sets ADOdb debug mode, defaults to false.

You can get going very fast:

$this->load->library('adodb');
$row = $this->adodb->getrow('SELECT * FROM users WHERE user_id = 1');
echo 'hello '.$row['firstname'];

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

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

Notes

Patch Notice

Note that you need the patch described at this thread 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.

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 active record stuff, but it may be possible to integrate it with models somehow.

Category:Contributions::Libraries::Database

Clone this wiki locally