There are a lot of tutorials in the web regarding this subject, but I think this is the fastest and the easiest way to run a ORM solution with CodeIgniter.

How

1. Download (if you haven’t already) CodeIgniter
2. Download Doctrine from here
3. Extract CodeIgniter to your web root.
4. Extract Doctrine folder and Doctrine.php in system/plugins folder of your Coeigniter install.
5. Create a file with following content and save it in the same folder and name it as doctrine_pi.php:

 $db_values) {

	// first we must convert to dsn format
	$dsn = $db[$connection_name]['dbdriver'] .
		'://' . $db[$connection_name]['username'] .
		':' . $db[$connection_name]['password'].
		'@' . $db[$connection_name]['hostname'] .
		'/' . $db[$connection_name]['database'];

	Doctrine_Manager::connection($dsn,$connection_name);
}

// CodeIgniter's Model class needs to be loaded
require_once BASEPATH.'/libraries/Model.php';

// telling Doctrine where our models are located
Doctrine::loadModels(APPPATH.'/models');

// (OPTIONAL) CONFIGURATION BELOW

// this will allow us to use "mutators"
Doctrine_Manager::getInstance()->setAttribute(
	Doctrine::ATTR_AUTO_ACCESSOR_OVERRIDE, true);

// this sets all table columns to notnull and unsigned (for ints) by default
Doctrine_Manager::getInstance()->setAttribute(
	Doctrine::ATTR_DEFAULT_COLUMN_OPTIONS,
	array('notnull' => true, 'unsigned' => true));

// set the default primary key to be named 'id', integer, 4 bytes
Doctrine_Manager::getInstance()->setAttribute(
	Doctrine::ATTR_DEFAULT_IDENTIFIER_OPTIONS,
	array('name' => 'id', 'type' => 'integer', 'length' => 4));

6. Integration is ready.
7. Go to your system/application/config folder and edit:
7.1 autoload.php:

$autoload['plugin'] = array('doctrine');

7.2 database.php – fill your data
8. We are ready to make our first example

Why?

Doctrine is an Object Relational Mapper for PHP. It basically means that you can map your database tables to classes in your web application. You don’t need Models anymore and you don’t have to write SQL anyore, just use ORM.

Simple controller

username = 'johndoe';
		$u->password = 'secret';
		$u->first_name = 'John';
		$u->last_name = 'Doe';
		$u->save();


	
	}


}

Here is the ORM model. Just few definitions, without a single track of SQL:)

class User extends Doctrine_Record {

	public function setTableDefinition() {
		$this->hasColumn('username', 'string', 255);
		$this->hasColumn('password', 'string', 255);
		$this->hasColumn('first_name', 'string', 255);
		$this->hasColumn('last_name', 'string', 255);
	}

}

This post was inspired by this tutorial here. You can see it, if you are interested in learning ORM from scratch with Doctrine and CodeIgniter, but PLEASE use this setup instead of described by other author..

3 thoughts on “How to Run Doctrine ORM with CodeIgniter

  1. You know i’ve spent a bit of time looking at Doctrine and the trickiest part was installing it.

    This explained how to do it in 2 mins.

    Bless you.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.