How to Run Doctrine ORM with CodeIgniter

September 20, 2010
By Bogomil Shopov
3 Comments
Post Image

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..

Bogomil Shopov

I care about privacy, ethical design, and freedom in many aspects. I spend 20+ years working as a web developer and architect, analyst, manager, and product owner/manager in different environments, several countries, and multiple software industries like Healthcare and Hospitality. I wore many hats, and I use the knowledge gathered to optimize the flow of value across complex systems.

3 Comments

  • Brodie

    November 8, 2010 at 6:58 am

    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.

    Reply
    • Bogo

      November 8, 2010 at 12:22 pm

      You are welcome :)

      Reply
  • Dada

    March 30, 2017 at 11:57 am

    I want complete setup in Codeigniter 2 & Doctrine 2

    Reply

Leave a reply

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