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
require_once('./CodeIgniterBridge/Bridge.php');

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

// include CodeIgniter DB
require_once(BASEPATH.'database/DB.php');
$db = DB($config);

// custom factory class to instantiate DB_Forge
require_once(BRIDGE.'DBForgeFactory.php');
$dbforge = DBForgeFactory::Factory($db);

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

// 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());

$db->close();

Download: codeigniter_activerecord_172.zip

Leave a Reply