Last Updated: August 24, 2017
·
10.54K
· 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:
Common,
DBAL and
ORM

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

<?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

<?php
/**
 * Doctrine boostrap file for CodeIgniter
 */

use Doctrine\Common\ClassLoader,
    Doctrine\Common\Cache\ArrayCache,
    Doctrine\Common\Annotations\AnnotationRegistry,
    Doctrine\Common\Annotations\AnnotationReader,
    Doctrine\ORM\Tools\Setup,
    Doctrine\ORM\EntityManager,
    Doctrine\ORM\Mapping\Driver\AnnotationDriver;

$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__);
$doctrineClassLoader->register();

// 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
AnnotationRegistry::registerLoader('class_exists');
$config->setMetadataDriverImpl($driver);
$config->setMetadataCacheImpl($cacheImpl);
$config->setQueryCacheImpl($cacheImpl);

$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

<?php

// Add 'Doctrine' library to auto-load
$autoload['libraries'] = array(
    'session',
    'database',
    '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');
$user->setUserEmail('my@email.com');
$em->persist($user);
$em->flush();

Regards.

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

Regards

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'
(include_path='.;C:\php\pear')

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 ·