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 »

Dec 282011
 

In this post I will show you how to integrate Smarty 3 (I use the current version Smarty 3.1.3 ) with CodeIgniter 2 (2.0.3).

1. Create CodeIgniter application ready to integrate Smarty

Create a folder name codeigniter-smarty and copy codeigniter lib folder to it. You will make something like this.

codeigniter smarty integration thumb CodeIgniter 2 Smarty 3 integration

2. Download Smarty and prepare library for CodeIgniter

Now you will need to download Smarty and copy its libs folder.

smarty lib for codeigniter thumb CodeIgniter 2 Smarty 3 integration

In your CodeIgniter application folder, find the libraries folder and create a folder name “smarty” and a file name smarty.php. Don’t worry about their content, we will complete it softly, now just paste your libs folder copy from Smarty to libraries > smarty folder

codeigniter smarty integration folder thumb CodeIgniter 2 Smarty 3 integration

You will also need a cache and a config folder, please create them in the librariessmarty as following:

image thumb3 CodeIgniter 2 Smarty 3 integration

Continue reading »