Note
This document is still a work in progress so you may encounter incomplete and moving sections.
Important
The addresses mentioned in this file may change!
Important
The project is not yet hosted on a server, so availability may differ from time to time.

1. Requirements

2. Introduction

Why use CodeIgniter. Why use Doctrine? And why use this version? These are the questions that will be answered in this section.

Using CodeIgniter saves you a lot of time not having to write your own framework. Having a framework considerately saves on application development times and decreases the amount of work needed to squashing bugs as adding functionality is a simple process which involves just adding some files and a little bit of extra code and you are up.

With Doctrine this process can even get an extra speed bump. Now you won’t have to type your models yourself anymore. The only thing you need to do is to supply a database or an YAML file. Then Doctrine can read your database or YAML file and create the necessary files for you.

2.1. Provided Versions

3. Installing

SVN: https://projects.spockz.nl/svn/doctrinecodeigniter

Installing this package is actually quite simple. If you have an already running copy of CodeIgniter you’ll only need to check out the /libraries folder, together with a copy of /application.

For a complete fresh install you can do a checkout of the full SVN tree. This can take quite a while as it will do external checkouts of CodeIgniter and Doctrine.

3.1. Configuring

In the file application/config/database.php look for the following code block:

$active_group = "default";
$active_record = TRUE;

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "doctrine";
$db['default']['password'] = "doctrinetest";
$db['default']['database'] = "doctrinetest";
$db['default']['dbdriver'] = "postgre";
$db['default']['dbDoctrineDriver'] = "pgsql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = TRUE; // Optional
$db['default']['cache_on'] = FALSE; // Optional
$db['default']['cachedir'] = ""; // Optional
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";

Replace the applicable variables to suit your environment.

Note
There are two fields for specifying the driver, one for the CI AR and one for Doctrine. This is because of naming differences between the CI and Doctrine libraries. Due to known issue #1 it is also not yet possible to completely throw out the CI ActiveRecord.

3.2. Using a fresh database

3.3. Using an existing database

Follow all the steps from above and then be sure to look at the following page: Generating Models from an existing Database

3.4. The batch file

## This is an install script for Doctrine on CodeIgniter.
## Totally _NO_ warranties provided.
if %1 then

fi

4. The command-line interface

In /application there is the file doctrine.php. This is the command-line interface for Doctrine.

5. Known Problems

  1. The CI_Session lib hardcoded uses the CI Active Record. So if you want to use the Session lib provided by CI you need to let both CI and Doctrine make connections to your database.

6. TODO

  1. Consult with CodeIgniter team on how to solve the Session problem before starting to rewrite the whole lot. See also.

  2. Potentially rewrite the whole lot. :)

7. How did I do this?

Quite the only things I did was adding a Doctrine loading / initialization hook, adding some fields to the database config and placing a CI front-end for the Doctrine CLI. Credits to the people who made the things.

7.1. The Hook

Changes to the original:

<?php
/**
  * @author maulin (http://blog.medryx.org/2008/10/04/codeigniter-and-doctrine/)
  * @author Alessandro Vermeulen <avermeulen@spockz.nl>
  */
// YOU MUST EDIT THIS TO BE THE PATH TO YOUR DOCTRINE LIBRARY
// I put mine in a 'libs' folder at the same level as the
// CI application folder, but it can be anywhere.
require_once APPPATH       . DIRECTORY_SEPARATOR .
                                '..'       . DIRECTORY_SEPARATOR .
                                'libraries'. DIRECTORY_SEPARATOR .
                                'doctrine' . DIRECTORY_SEPARATOR .
                                'lib' . DIRECTORY_SEPARATOR .
                                'Doctrine.php';

function bootstrap_doctrine() {
        include APPPATH . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'database' . EXT;

        // Set the autoloader
        spl_autoload_register(array('Doctrine', 'autoload'));

        //optional, you can set this to whatever you want, or not set it at all
        Doctrine_Manager::getInstance()->setAttribute('model_loading', 'conservative');

        // Load the Doctrine connection
        // (Notice the use of $active_group here, to make it easy to swap out
        //  you connection based on you database.php configs)

        if (!isset($db[$active_group]['dsn'])) {
                //try to create the dsn, if it has not been manually set
                //in your config. I personally would opt to set my
                //dsn manually, but it works either way
                $db[$active_group]['dsn'] = $db[$active_group]['dbdriver'] .
                        '://' . $db[$active_group]['username'] .
                        ':' . $db[$active_group]['password'].
                        '@' . $db[$active_group]['hostname'] .
                        '/' . $db[$active_group]['database'];
        }

        Doctrine_Manager::connection($db[$active_group]['dsn']);

        // Load the models for the autoloader
        // This assumes all of your models will exist in you
        // application/models folder
        Doctrine::loadModels(APPPATH . DIRECTORY_SEPARATOR . 'models', Doctrine::MODEL_LOADING_CONSERVATIVE);
}
?>

7.2. The database config

Changes to the original:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------
| DATABASE CONNECTIVITY SETTINGS
| -------------------------------------------------------------------
| This file will contain the settings needed to access your database.
|
| For complete instructions please consult the "Database Connection"
| page of the User Guide.
|
| -------------------------------------------------------------------
| EXPLANATION OF VARIABLES
| -------------------------------------------------------------------
|
|       ['hostname'] The hostname of your database server.
|       ['username'] The username used to connect to the database
|       ['password'] The password used to connect to the database
|       ['database'] The name of the database you want to connect to
|       ['dbdriver'] The database type. ie: mysql.  Currently supported:
                                 mysql, mysqli, postgre, odbc, mssql, sqlite, oci8
|       ['dbprefix'] You can add an optional prefix, which will be added
|                                to the table name when using the  Active Record class
|       ['pconnect'] TRUE/FALSE - Whether to use a persistent connection
|       ['db_debug'] TRUE/FALSE - Whether database errors should be displayed.
|       ['cache_on'] TRUE/FALSE - Enables/disables query caching
|       ['cachedir'] The path to the folder where cache files should be stored
|       ['char_set'] The character set used in communicating with the database
|       ['dbcollat'] The character collation used in communicating with the database
|
| The $active_group variable lets you choose which connection group to
| make active.  By default there is only one group (the "default" group).
|
| The $active_record variables lets you determine whether or not to load
| the active record class
*/

$active_group = "default";
$active_record = TRUE;

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "doctrine";
$db['default']['password'] = "doctrinetest";
$db['default']['database'] = "doctrinetest";
$db['default']['dbdriver'] = "postgre";
$db['default']['dbDoctrineDriver'] = "pgsql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_unicode_ci";


// Create dsn from the info above
$db['default']['dsn'] = $db['default']['dbDoctrineDriver'] .
                        '://' . $db['default']['username'] .
                        ':' . $db['default']['password'].
                        '@' . $db['default']['hostname'] .
                        '/' . $db['default']['database'];

/* End of file database.php */
/* Location: ./system/application/config/database.php */

7.3. The Frontend

Changes to the original:

<?php
/**
 * This file contains some hijacked things to provide the CLI entrance to
 * doctrine.
 */
define('BASEPATH', null);
require_once('config/database.php');
require_once('../libraries/doctrine/lib/Doctrine.php');

//register the autoloader
spl_autoload_register(array('Doctrine', 'autoload'));

// Configure Doctrine Cli
// Normally these are arguments to the cli tasks but if they are set here the arguments will be auto-filled
$config = array('data_fixtures_path'  =>  dirname(__FILE__) . DIRECTORY_SEPARATOR . 'fixtures',
                'models_path'         =>  dirname(__FILE__) . DIRECTORY_SEPARATOR . 'models',
                'migratons_path'     =>  dirname(__FILE__) . DIRECTORY_SEPARATOR . 'migrations',
                'sql_path'            =>  dirname(__FILE__) . DIRECTORY_SEPARATOR . 'sql',
                'yaml_schema_path'    =>  dirname(__FILE__) . DIRECTORY_SEPARATOR . 'schema');

// Load the Doctrine connection
Doctrine_Manager::connection($db['default']['dsn'], $db['default']['database']);

$cli = new Doctrine_Cli($config);
$cli->run($_SERVER['argv']);
?>

8. Remarks

Be sure to not create two classes with the same name, i.e. a User controller and a User model. This is one of the first things I stumbled upon when I first started using Doctrine with CodeIgniter.