Medryx Observations

October 4, 2008

CodeIgniter and Doctrine

Filed under: PHP — Tags: , , , — maulin @ 9:47 am

The most widely sited source for how to bootstrap Doctrine with CodeIgniter is available on the CI Wiki. While this approach is effective, I found it a bit intrusive into the CI code. I didn’t like the idea of having to edit index.php, and I didn’t like that the database.php, which should really only contain configuration code, was hijacked to actually do the bootstrapping. So I looked into it a bit, and I think there is a cleaner (if not easier) way.

The goal when bootstrapping Doctrine is to load up your database connection and point to your models folder. Thats pretty much it. Once you do that, all your models autoload, so you don’t need to do things like $this->load->mode('User'). Instead you can simply use Doctrine’s tools such as new User() or Doctrine::getTable('User').

So here is the process:

1. Install a “hook” that will bootstrap Doctrine. Edit application/config/hooks.php and add the following:

$hook['pre_controller'][] = array(
	'function' => 'bootstrap_doctrine',
	'filename' => 'doctrine.php',
	'filepath' => 'hooks'
);

2. Copy the following to application/hooks/doctrine.php

// YOU MUST EDIT THIS TO BE THE PATH TO YOUR DOCTRINE LIBRARY
// I put mine in a 'libs' folder at the same level as the 
// CI application folder, but it can be anywhere.
require_once APPPATH   	   . DIRECTORY_SEPARATOR . 
				'..' 	   . DIRECTORY_SEPARATOR . 
				'libs'     . DIRECTORY_SEPARATOR . 
				'Doctrine' . DIRECTORY_SEPARATOR . 
				'Doctrine.php';
 
function bootstrap_doctrine() {
 
	@include APPPATH . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'database' . EXT;
 
	// Set the autoloader
	spl_autoload_register(array('Doctrine', 'autoload'));
 
	//optional, you can set this to whatever you want, or not set it at all
	Doctrine_Manager::getInstance()->setAttribute('model_loading', 'aggressive');
 
	// Load the Doctrine connection
	// (Notice the use of $active_group here, to make it easy to swap out
	//  you connection based on you database.php configs)
 
	if (!isset($db[$active_group]['dsn'])) {
		//try to create the dsn, if it has not been manually set
		//in your config. I personally would opt to set my
		//dsn manually, but it works either way
		$db[$active_group]['dsn'] = $db[$active_group]['dbdriver'] . 
                        '://' . $db[$active_group]['username'] . 
                        ':' . $db[$active_group]['password']. 
                        '@' . $db[$active_group]['hostname'] . 
                        '/' . $db[$active_group]['database'];
	}
 
	Doctrine_Manager::connection($db[$active_group]['dsn']);
 
	// Load the models for the autoloader
	// This assumes all of your models will exist in you
	// application/models folder
	Doctrine::loadModels(APPPATH . DIRECTORY_SEPARATOR . 'models');
}

3. Done. Doctrine and its models are now available in the usual Doctrine way in all of your controllers.

Let me know if you have questions!

Powered by WordPress