Last Updated: August 24, 2017
· joseluisq

How to configure Doctrine 2 with CodeIgniter PHP

For working with Doctrine 2 Entities in CodeIgniter Framework, follow the steps below.<br>

First, copy the Doctrine 2 framework directory to /application/libraries/Doctrine<br>
Note: Your Doctrine directory should have the three sub-directories:
DBAL and

Second, create the Doctrine.php class at /application/libraries/Doctrine.php


 * Doctrine library for CodeIgniter
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;

class Doctrine {

     * @var EntityManager Doctrine Entity Manager
    private $entityManager = NULL;

    public function __construct() {
        require_once 'bootstrap.php';
        $this->entityManager = EntityManager::create($conn, $config);

     * @return EntityManager Doctrine Entity Manager
    public function getEntityManager() {
        return $this->entityManager;


Third, create the boostrap.php file at /application/libraries/boostrap.php

 * Doctrine boostrap file for CodeIgniter

use Doctrine\Common\ClassLoader,

$PATH_APP = dirname(__DIR__);
$PATH_DOCTRINE = 'Doctrine';

include $PATH_APP . '/config/database.php';
require_once $PATH_DOCTRINE . '/Common/ClassLoader.php';

$doctrineClassLoader = new ClassLoader('Doctrine', __DIR__);

// Replace it with your environment
$applicationMode = 'development';

$isDevMode = FALSE;
$paths = array(APPPATH . '/models');
$cacheImpl = $applicationMode == 'development' ? new ArrayCache : new Doctrine\Common\Cache\ApcCache;

// Configuration
$config = Setup::createConfiguration($isDevMode);
$driver = new AnnotationDriver(new AnnotationReader(), $paths);

// Registering noop annotation autoloader - allow all annotations by default

$conn = array(
    'host' => $db['default']['hostname'],
    'driver' => 'pdo_' . $db['default']['dbdriver'],
    'user' => $db['default']['username'],
    'password' => $db['default']['password'],
    'dbname' => $db['default']['database'],
    'port' => $db['default']['port'],
    'charset' => $db['default']['char_set'],

$entityManager = EntityManager::create($conn, $config);

Fourth, add the Doctrine library to autoload.php file located at /application/config


// Add 'Doctrine' library to auto-load
$autoload['libraries'] = array(
    'Doctrine' // Add this

Finally, Start to work with your entities in your controller.<br>
Note: I this case I'm using an entity called 'User' (replace with your own entity class)

// Include your model(s)
include_once '/application/models/User.php';

// Get EntityManager Instance
$em = $this->doctrine->getEntityManager();

// Working with my User entity for insert
$user = new User();
$user->setUserName('My Name');


3 Responses
Add your response

Hi dear,

I'm new in doctrine. And I'm with one question, how I need create this class user.php in models, there is a template or doctrine generate this?

over 1 year ago ·

Hi, yeah exists many ways to create a model structures.<br>
So, you can create a models manually or via console more info about Doctrine Console<br>
In my case, I prefer to create my models via bash commands.

Here a sample script to generate models from database:<br>
- Shell script for Linux<br>
- Batch script for Windows


over 1 year ago ·

hi ,
after trying to configure the doctrine with codeigniter it produces the follwing error
requireonce(Doctrine/Common/ClassLoader.php): failed to open stream: No such file or directory
Message: require
once(): Failed opening required 'Doctrine/Common/ClassLoader.php'

Message: Cannot modify header information - headers already sent by (output started at D:\wamp64\www\tryTry\application\libraries\bootstrap.php:18)

thanks in advance

over 1 year ago ·