Using CodeIgniter’s ActiveRecord implementation standalone

I’ve recently been looking into PHP ORM frameworks. One framework that scored highly was CodeIgniter’s implementation of the ActiveRecord pattern.
Unfortunately, the code cannot be used in standalone projects- you’re tied to using the full CodeIgniter MVC approach.
Looking for standalone versions of CI’s database classes I found several attempts, though none very successful or limited to mySQL as only database.
I’ve therefore developed my own adaption. The code is based on latest CodeIgniter 1.7.2 and consists of the largely unmodified CI classes and some additional bridge code.

The small library provided is straightforward to use:

// include bridge functionality

$config = array('username'=> 'root',
'password'=> '',
'database'=> 'test.db3',
'dbdriver'=> 'sqlite');

// include CodeIgniter DB
$db = DB($config);

// custom factory class to instantiate DB_Forge
$dbforge = DBForgeFactory::Factory($db);

// CodeIgniter - create DB
$dbforge->add_field(array('title' => array('type' => 'VARCHAR')));

// CodeIgniter - create and show data
$db->insert('test', array('title' => 'A'));
$db->insert('test', array('title' => 'B'));
var_dump($db->select('title')->from('test')->where('title LIKE', 'A%')->get()->result_array());



Leave a Reply