Skip to content

Using Doctrine with Code Igniter Conventions

World Wide Web Server edited this page Jul 4, 2012 · 30 revisions

[h3]1. Download Doctrine into the application/libraries folder.[/h3]

[code] cd /path/to/ci/install/ svn co http://svn.doctrine-project.org/tags/1.1.0-RC1 system/application/libraries/doctrine [/code]

This should download the entire doctrine project with tools, vendor and docs directories. This will allow you to run the command line out of the sandbox directory. If you're running off a different release or have a different directory structure at the very least you need the following Doctrine files:

[code] system/application/libraries/doctrine/lib/Doctrine.php system/application/libraries/doctrine/lib/Doctrine/ [/code]

[h3]2. Add the config file[/h3]

Create a new file in system/application/config/ named doctrine.php. This file should contain the following:

[code] define('DOCTRINE_PATH', APPPATH . 'libraries' . DIRECTORY_SEPARATOR . 'doctrine' . DIRECTORY_SEPARATOR . 'lib'); define('DATA_FIXTURES_PATH', APPPATH . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'fixtures'); define('MODELS_PATH', APPPATH . 'models'); define('MIGRATIONS_PATH', APPPATH . 'MIGRATIONS'); define('SQL_PATH', APPPATH . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'sql'); define('YAML_SCHEMA_PATH', APPPATH . DIRECTORY_SEPARATOR . 'schema');

require_once(DOCTRINE_PATH . DIRECTORY_SEPARATOR . 'Doctrine.php');

spl_autoload_register(array('Doctrine', 'autoload'));

Doctrine_Manager::connection('mysql://root:root@127.0.0.1/test'); Doctrine_Manager::getInstance()->setAttribute('model_loading', 'conservative'); [/code]

This is pieced together from the sandbox example and uses the default MAMP database settings. You'll probably need to change at least the MySQL settings but possibly the paths as well.

[h3]2. Install the custom loader[/h3]

In system/application/libraries/ you'll need to edit MY_Loader.php. If the file doesn't exist create it with the following shell:

[code] class MY_Loader extends CI_Loader { } [/code]

Then add the doctrine() method to the MY_Loader class.

[code] class MY_Loader extends CI_Loader { function doctrine($table=FALSE) { if (!defined('DOCTRINE_PATH')) { require APPPATH.'config/doctrine.php'; }

    if ($table !== FALSE) {
        $ci =&get;_instance();
        Doctrine::loadModels(MODELS_PATH);
        
        $ci->{$table} = Doctrine::getTable($table);
        return $ci->{$table};
    }
    
    return new Doctrine_Migration;
}

} [/code]

Clone this wiki locally