<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Kevin Bradwick &#187; ORM</title>
	<atom:link href="http://www.kevinbradwick.co.uk/tag/orm/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kevinbradwick.co.uk</link>
	<description>Web development and design blog</description>
	<lastBuildDate>Fri, 09 Jul 2010 08:11:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Integrating CodeIgniter and Doctrine</title>
		<link>http://www.kevinbradwick.co.uk/2009/12/integrating-codeigniter-and-doctrine/</link>
		<comments>http://www.kevinbradwick.co.uk/2009/12/integrating-codeigniter-and-doctrine/#comments</comments>
		<pubDate>Mon, 28 Dec 2009 23:05:06 +0000</pubDate>
		<dc:creator>kevin</dc:creator>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[Doctrine]]></category>
		<category><![CDATA[ORM]]></category>

		<guid isPermaLink="false">http://www.kevinbradwick.co.uk/?p=5</guid>
		<description><![CDATA[Having used CodeIgniter over the past year or so, I came to realise some limitations with the Database library. CodeIgniter does not have a proper ORM library like a lot of other frameworks provide. With an ORM library, you have a lot more tools at your disposal to not only increase productivity but also improve [...]]]></description>
			<content:encoded><![CDATA[<p>Having used <a href="http://www.codeigniter.com">CodeIgniter</a> over the past year or so, I came to realise some limitations with the Database library. CodeIgniter does not have a proper ORM library like a lot of other frameworks provide. With an ORM library, you have a lot more tools at your disposal to not only increase productivity but also improve database transaction efficiency.</p>
<p><a href="http://www.doctrine-project.org">Doctrine</a> is a full blown PHP ORM framework that can be integrated into other frameworks or old school PHP applications. Here is how to integrate the framework to CodeIgniter.</p>
<p><a href="http://www.doctrine-project.org/download">Download</a> a copy of the Doctrine framework and place it somewhere in your web directory (for this tutorial I am placing it at the web root directory /). My directory structure looks like;</p>
<pre name="code" class="php">
.
..
.htaccess
index.php
licence.txt
user_guide
system
doctrine
application
</pre>
<h3>Set database credentials</h3>
<p>As you would with any other CI app that utilises a database, enter the connection details in the database.php file inside of the config folder.</p>
<pre name="code" class="php">
//.. application/config/database.php
$db['default']['hostname'] = "[hostname]";
$db['default']['username'] = "[username]";
$db['default']['password'] = "[password]";
$db['default']['database'] = "[databasename]";
$db['default']['dbdriver'] = "[driver e.g. mysql]";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";
</pre>
<h3>Enable hooks</h3>
<p>In the config file [application/config/config.php] enable hooks by setting hooks to TRUE.</p>
<pre class="php" name="code">
//.. application/config/config.php
$config['enable_hooks'] = TRUE;
</pre>
<h3>Insert the hook</h3>
<p>Create a new file in the hooks directory [application/hooks] called doctrine.php</p>
<pre name="code" class="php">
//.. application/hooks/doctrine.php
if(!defined('BASEPATH')) exit('file locked');

require_once( FCPATH . 'doctrine/lib/Doctrine.php' );

class DoctrineHook
{

	function bootstrap_doctrine()
	{
		require_once( APPPATH . '/config/database.php' );

		spl_autoload_register(array('Doctrine', 'autoload'));
		Doctrine_Manager::getInstance()->setAttribute('model_loading', 'aggressive');

		if (!isset($db[$active_group]['dsn'])) {
			$db[$active_group]['dsn'] = $db[$active_group]['dbdriver'] .
							'://' . $db[$active_group]['username'] .
							':' . $db[$active_group]['password'].
							'@' . $db[$active_group]['hostname'] .
							'/' . $db[$active_group]['database'];
		}

		Doctrine_Manager::connection($db[$active_group]['dsn']);
		Doctrine_Manager::setAttribute(Doctrine_Core::ATTR_AUTO_ACCESSOR_OVERRIDE, true);
		Doctrine_Manager::setAttribute(Doctrine_Core::ATTR_USE_NATIVE_ENUM, true);
		Doctrine::loadModels(APPPATH . DIRECTORY_SEPARATOR . 'models/doctrine/');
	}

}
</pre>
<h3>Register the hook</h3>
<p>So, now we have the hook in place we need to tell CI to execute the hook on system start up. Over to application/config/hooks.php&#8230;</p>
<pre name="code" class="php">
//.. application/config/hooks.php
 if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| Hooks
| -------------------------------------------------------------------------
| This file lets you define "hooks" to extend CI without hacking the core
| files.  Please see the user guide for info:
|
|	http://codeigniter.com/user_guide/general/hooks.html
|
*/
$hook['pre_controller'][] = array(
	'class' => 'DoctrineHook',
	'function' => 'bootstrap_doctrine',
	'filename' => 'doctrine.php',
	'filepath' => 'hooks'
	);

/* End of file hooks.php */
/* Location: ./system/application/config/hooks.php */
</pre>
<p>With this now in place, CI will load doctrine upon each load. There is one thing left to do. Some of you may have noticed in the doctrine.php hook we created, we told Doctrine to load our models from application/models/doctrine. I created the <em>doctrine</em> folder inside the models folder to purposely seperate doctrine classes from my CI models.</p>
<p>Everything should now work and you can call a model using native PHP class loading methods&#8230;</p>
<pre name="code" class="php">
//.. inside a controller we may call a user class like this;
$user = new User();
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.kevinbradwick.co.uk/2009/12/integrating-codeigniter-and-doctrine/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
