Q:

Integrating Doctrine with CodeIgniter

I have successfully installed the latest version of CodeIgniter and have basic MVC pattern working. The problem that I've noticed is that CI doesn't naturally allow for prepared statements when it comes to queries. So, I decided to download Doctrine 1 from GitHub. I'm very new to Doctrine and needed some help integrating it with CI so I followed this tutorial. 
In one of my controllers, I have
$this->load->library('doctrine');
$this->em = $this->doctrine->em;

But, when I go to load the view in my browser, I'm greeted with an error reading 

Message: require_once(/Applications/MAMP/htdocs/CodeIgniter/application/libraries/Doctrine/Common/ClassLoader.php): failed to open stream: No such file or directory

Upon further inspection of the Doctrine download from GitHub, there doesn't even seem to be a folder titled "common" anywhere in there. I'm very new to CI and especially Doctrine. Does anyone have some advice that can help me get this working? Also, is it possible to use the MySQLi driver instead of the PDO one with Doctrine? 

A:

Downloading the Doctrine ORM straight from GitHub doesn't include the other dependencies. These are managed by Composer. If you look inside the composer.json file you can see these dependencies. If you want to install them manually, they are:

doctrine/common
doctrine/inflector
doctrine/cache
doctrine/collections
doctrine/lexer
doctrine/annotations
doctrine/dbal
symfony/console

I believe that's all of them. You will have to merge these files in their appropriate directories as they follow PSR-0 standards for the autoloading of classes.
Alternatively, install Doctrine 2 with Composer with the following composer.json file and any other dependencies will be installed automatically. Then integrate with CodeIgniter.
{
    "minimum-stability": "stable",
    "require": {
        "doctrine/orm": "2.3.*"
    }
}

Edit the index.php file of your CodeIgniter app by adding a single line to include the autoloader file before requiring the CodeIgniter core.
require_once BASEPATH.'../vendor/autoload.php';

require_once BASEPATH.'core/CodeIgniter.php';

Also if installing with Composer, use this edited version of the bootstrap as the contents of application/libraries/Doctrine.php, which is what worked for me
<?php

use Doctrine\Common\ClassLoader,
    Doctrine\ORM\Tools\Setup,
    Doctrine\ORM\EntityManager;

class Doctrine
{
    public $em;

    public function __construct()
    {
        // Load the database configuration from CodeIgniter
        require APPPATH . 'config/database.php';

        $connection_options = array(
            'driver'        => 'pdo_mysql',
            'user'          => $db['default']['username'],
            'password'      => $db['default']['password'],
            'host'          => $db['default']['hostname'],
            'dbname'        => $db['default']['database'],
            'charset'       => $db['default']['char_set'],
            'driverOptions' => array(
                'charset'   => $db['default']['char_set'],
            ),
        );

        // With this configuration, your model files need to be in application/models/Entity
        // e.g. Creating a new Entity\User loads the class from application/models/Entity/User.php
        $models_namespace = 'Entity';
        $models_path = APPPATH . 'models';
        $proxies_dir = APPPATH . 'models/Proxies';
        $metadata_paths = array(APPPATH . 'models');

        // Set $dev_mode to TRUE to disable caching while you develop
        $config = Setup::createAnnotationMetadataConfiguration($metadata_paths, $dev_mode = true, $proxies_dir);
        $this->em = EntityManager::create($connection_options, $config);

        $loader = new ClassLoader($models_namespace, $models_path);
        $loader->register();
    }
}

Note: Version 3 of CodeIgniter when released, will be installable with Composer, but version 2 is not.

