-
Notifications
You must be signed in to change notification settings - Fork 0
Using Doctrine with Code Igniter Conventions
[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.
[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) { 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(MIGRATIONS_PATH);
}
} [/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 up 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]