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:[email protected]/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. Additionally if you plan on using the CLI then you'll want to replace out all instances of APPPATH with the full path.

[h3]3. 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, once you have a MY_Loader.php add the doctrine() method to the MY_Loader class. After you're done it should resemble the following:

[code] class MY_Loader extends CI_Loader { function doctrine($table=FALSE) { $ci =&get;_instance();

    if (!defined('DOCTRINE_PATH')) {
        require APPPATH.'config/doctrine.php';
    }
    
    if ($table !== FALSE) {
        if ($ci->{$table}) {
            return $ci->{$table};
        }
        
        Doctrine::loadModels(MODELS_PATH);
        
        $ci->{$table} = Doctrine::getTable($table);
        return $ci->{$table};
    }
    
    return FALSE;
}

} [/code]

[h3]4. Using Doctrine[/h3]

Now that Doctrine is installed you'll have to set up your database tables and models manually. You can read the documentation to use the command line to automatically create things via YML, but in effect you'll need something like the following in your model (you should be able to guess the MySQL table based on the hasColumn calls):

[code] class Song extends Doctrine_Record { public function setTableDefinition() { $this->setTableName('library'); $this->hasColumn('id', 'integer', 4, array('type' => 'integer', 'length' => 4, 'primary' => true, 'autoincrement' => true)); $this->hasColumn('artist', 'string', 255, array('type' => 'string', 'length' => 255)); $this->hasColumn('song', 'string', 255, array('type' => 'string', 'length' => 255)); }

public function setUp() {
    
}

} [/code]

With that set up you can use Song in your controller something like this:

[code] class Welcome extends Controller { function index() { $this->load->doctrine('song'); $this->song->findAll(); $song = $this->song->find(1); $song->artist; $this->song->find(1)->artist; }

} [/code]

[h3]5. Using Migrations[/h3]

First you'll need to set up the command line interface of Doctrine by creating a file named doctrine in your system/application directory. In it place the following code. ALSO, make sure you replaced out all instances of APPPATH in your config/doctrine.php with the full application path as noted above.

[code] #!/usr/bin/env php <?php chdir(dirname(FILE));

require_once('config/doctrine.php');

// 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' => DATA_FIXTURES_PATH, 'models_path' => MODELS_PATH, 'migrations_path' => MIGRATIONS_PATH, 'sql_path' => SQL_PATH, 'yaml_schema_path' => YAML_SCHEMA_PATH);

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

Then you'll need to create a database table to store the current migrated version, so run this query on your CI database:

[code] CREATE TABLE migration_version ( version int(11) default NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; [/code]

Finally, create the actual migration in a file named 001_create_songs.class.php inside system/application/migrations/ and add the following:

[code] <?php

class CreateSongs extends Doctrine_Migration_Base { public function up() { $this->createTable('songs', array('title' => array('type' => 'string'))); }

public function postUp() {
    $migrationTest = new MigrationTest();
    $migrationTest->title = 'We Built This City';
    $migrationTest->save();
}

public function down() {
    $this->dropTable('songs');
}

public function postDown() {
    $migrationTest = Doctrine::getTable('songs')->findOneByTitle('We Built This City');
    $migrationTest->delete();
}

} [/code]

Clone this wiki locally