Dec 282011
 

Doctrine – a very famous and strong ORM library for PHP web app development now can be easily integrated into CodeIgniter. (this post is assumed that you already know about PHP, CodeIgniter, ORM, Doctrine… so I just do the rest: to integrate them together)

1. Create CodeIgniter application ready to integrate Doctrine

You will need a folder name codeigniter-doctrine and copy the codeigniter application, system and index.php file into it. The result should look like this.

codeigniter doctrine integration thumb1 CodeIgniter 2 Doctrine 2 Integration

2. Download Doctrine library for CodeIgniter

Just download Doctrine ORM from it homepage, extract it and you will see the following:

image thumb7 CodeIgniter 2 Doctrine 2 Integration

We will need that Doctrine lib folder, copy it into CodeIgniter application libraries folder, and create a file name Doctrine.php just like this.

codeigniter doctrine integration 2 thumb1 CodeIgniter 2 Doctrine 2 Integration

3. Doctrine and CodeIgniter Integration

Now you’re ready to configure the Doctrine library for CodeIgniter, open the file Doctrine.php in your text editor and replace it with the following content:

<?php
use DoctrineCommonClassLoader,
DoctrineORMConfiguration,
DoctrineORMEntityManager,
DoctrineCommonCacheArrayCache,
DoctrineDBALLoggingEchoSQLLogger;

class Doctrine {

	public $em = null;

	public function __construct()
	{
		// load database configuration from CodeIgniter
		require_once APPPATH.'config/database.php';	

		// Set up class loading. You could use different autoloaders, provided by your favorite framework,
		// if you want to.
		require_once APPPATH.'libraries/Doctrine/Common/ClassLoader.php';

		$doctrineClassLoader = new ClassLoader('Doctrine',  APPPATH.'libraries');
		$doctrineClassLoader->register();
		$entitiesClassLoader = new ClassLoader('models', rtrim(APPPATH, "/" ));
		$entitiesClassLoader->register();
		$proxiesClassLoader = new ClassLoader('Proxies', APPPATH.'models/proxies');
		$proxiesClassLoader->register();

		// Set up caches
		$config = new Configuration;
		$cache = new ArrayCache;
		$config->setMetadataCacheImpl($cache);
		$driverImpl = $config->newDefaultAnnotationDriver(array(APPPATH.'models/Entities'));
		$config->setMetadataDriverImpl($driverImpl);
		$config->setQueryCacheImpl($cache);

		$config->setQueryCacheImpl($cache);

		// Proxy configuration
		$config->setProxyDir(APPPATH.'/models/proxies');
		$config->setProxyNamespace('Proxies');

		$config->setAutoGenerateProxyClasses( TRUE );

		// Database connection information				

		$connectionOptions = array( 
        'driver' => 'pdo_mysql',		
        'user' =>     'webapp',
        'password' => 'webapp',
        'host' =>     'localhost',
        'dbname' =>   'mydb'
		);

		// Create EntityManager
		$this->em = EntityManager::create($connectionOptions, $config);
	}
}

As you can see from the above, we do most Doctrine config in this file, please change the $connectionOptions to the right database information you’re using.

Continue reading »