For those looking for a tutorial on integrating Doctrine 2 with CodeIgniter, this question and other answers are deprecated (for CI 2). This is the new tutorial for CI 3 that I did and I checked the work:
How to install Doctrine 2 in CodeIgniter 3
I repeat it here.
Install Doctrine
ORMs Doctrine 2 Documentation - Installation and Setup
Doctrine can be established with Composer . Define the following requirement in the composer.json file:
{ "require": { "doctrine/orm": "*" } }
Then call the composition from the command line.
Integration with CodeIgniter
Doctrine 2 ORMs Documentation - Integration with CodeIgniter
Here are the steps: Add the php file to the system / application / libraries folder called Doctrine.php. This will be your wrapper / bootstrap for the D2 entity manager. Place the Doctrine folder (the one that contains Common, DBAL, and ORM) inside the third_party folder. If you want, open the config / autoload.php file and load your Doctrine library: $autoload['libraries'] = array('doctrine');
Creating the Doctrine CodeIgniter Library
Now, here is what your Doctrine.php file should look like. Customize it according to your needs.
<?php /** * Doctrine 2.4 bootstrap * */ use Doctrine\Common\ClassLoader, Doctrine\ORM\Configuration, Doctrine\ORM\EntityManager, Doctrine\Common\Cache\ArrayCache, Doctrine\DBAL\Logging\EchoSQLLogger; class Doctrine { public $em = null; public function __construct() { // load database configuration from CodeIgniter require_once APPPATH.'config/database.php'; // include Doctrine ClassLoader class require_once APPPATH.'third_party/Doctrine/Common/ClassLoader.php'; // load the Doctrine classes $doctrineClassLoader = new ClassLoader('Doctrine', APPPATH.'third_party'); $doctrineClassLoader->register(); // load the entities $entityClassLoader = new ClassLoader('Entities', APPPATH.'models'); $entityClassLoader->register(); // load the proxy entities $proxiesClassLoader = new ClassLoader('Proxies', APPPATH.'models/proxies'); $proxiesClassLoader->register(); // load Symfony2 classes // this is necessary for YAML mapping files and for Command Line Interface (cli-doctrine.php) $symfonyClassLoader = new ClassLoader('Symfony', APPPATH.'third_party/Doctrine'); $symfonyClassLoader->register(); // Set up the configuration $config = new Configuration; // Set up caches if(ENVIRONMENT == 'development') // set environment in index.php // set up simple array caching for development mode $cache = new \Doctrine\Common\Cache\ArrayCache; else // set up caching with APC for production mode $cache = new \Doctrine\Common\Cache\ApcCache; $config->setMetadataCacheImpl($cache); $config->setQueryCacheImpl($cache); // set up annotation driver $driver = new \Doctrine\ORM\Mapping\Driver\PHPDriver(APPPATH.'models/Mappings'); $config->setMetadataDriverImpl($driver); // Proxy configuration $config->setProxyDir(APPPATH.'/models/Proxies'); $config->setProxyNamespace('Proxies'); // Set up logger $logger = new EchoSQLLogger; $config->setSQLLogger($logger); $config->setAutoGenerateProxyClasses( TRUE ); // only for development // Database connection information $connectionOptions = array( 'driver' => 'pdo_mysql', 'user' => $db['default']['username'], 'password' => $db['default']['password'], 'host' => $db['default']['hostname'], 'dbname' => $db['default']['database'] ); // Create EntityManager, and store it for use in our CodeIgniter controllers $this->em = EntityManager::create($connectionOptions, $config); } }
Command line tool setup
Doctrine comes with several command line tools that are very useful during development.
Check if these lines exist in the Doctrine.php file to load Symfony classes to use command line tools (and for YAML mapping files):
$symfonyClassLoader = new ClassLoader('Symfony', APPPATH.'third_party/Doctrine'); $symfonyClassLoader->register();
You need to register your EntityManager applications in the console tool in order to use the tasks by creating the cli-doctrine.php file in the application directory with the following contents:
<?php define('APPPATH', dirname(__FILE__) . '/'); define('BASEPATH', APPPATH . '/../system/'); define('ENVIRONMENT', 'development'); require APPPATH.'libraries/Doctrine.php'; $doctrine = new Doctrine; $em = $doctrine->em; $helperSet = new \Symfony\Component\Console\Helper\HelperSet(array( 'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()), 'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em) )); \Doctrine\ORM\Tools\Console\ConsoleRunner::run($helperSet); ?>
Now run this script through the PHP command line and you will see a list of commands available to you.
php cli-doctrine.php
Creating display classes from the database:
php cli-doctrine.php orm:convert-mapping --from-database annotation models/Entities
if you get this error: Fatal error: call to undefined function Doctrine \ Common \ Cache \ apc_fetch () install the APC extension for PHP:
sudo apt-get install php-apc sudo /etc/init.d/apache2 restart
For production mode, you'll want to use a real caching system like APC, get rid of EchoSqlLogger and disable autoGenerateProxyClasses in Doctrine.php