UPDATE 28 Dec 2010: This module is now defunct. Please instead use the Better Doctrine 2 Module for Kohana 3.
For those of you who don’t already know, Doctrine 2 stable was just released. It features what is essentially a complete rewrite and paradigm shift from the 1.2 version and comes with a laundry list of improvements. You can read the official blog post here. To celebrate the occasion I decided to do up a quick Kohana 3 module. Info & specz below:
Firstly the configuration. You’ll need the following files & folders:
/doctrine/Entities (These are essentially (to my meager knowledge) your model files.)
/doctrine/Proxies
/modules/doctrine/classes/doctrine
/modules/doctrine/init.php
Download the latest copy of Doctrine 2 from here and extract into the /modules/doctrine/classes/doctrine folder.
Here’s /modules/doctrine/init.php
<?php use Doctrine\ORM\EntityManager, Doctrine\ORM\Configuration; class Doctrine { private static $_instance = null; private $_application_mode = 'development'; private $_em = array(); /* * Rename $conn_name to whatever you want your default DB connection to be */ public static function em( $conn_name = 'default' ) { if ( self::$_instance === null ) self::$_instance = new Doctrine(); return isset(self::$_instance->em[ $conn_name ]) ? self::$_instance->em[ $conn_name ] : reset(self::$_instance->_em); } public function __construct() { require __DIR__.'/classes/doctrine/Doctrine/Common/ClassLoader.php'; $classLoader = new \Doctrine\Common\ClassLoader('Doctrine', __DIR__.'/classes/doctrine'); $classLoader->register(); //This allows Doctrine-CLI tool & YAML mapping driver $classLoader = new \Doctrine\Common\ClassLoader('Symfony', __DIR__.'/classes/doctrine/Doctrine'); $classLoader->register(); //Load entities $classLoader = new \Doctrine\Common\ClassLoader('Entities', APPPATH.'doctrine'); $classLoader->register(); //Set up caching method $cache = $this->_application_mode == 'development' ? new \Doctrine\Common\Cache\ArrayCache : new \Doctrine\Common\Cache\ApcCache; $config = new Configuration; $config->setMetadataCacheImpl( $cache ); $driver = $config->newDefaultAnnotationDriver( APPPATH.'doctrine/Entities' ); $config->setMetadataDriverImpl( $driver ); $config->setQueryCacheImpl( $cache ); $config->setProxyDir( APPPATH.'doctrine/Proxies' ); $config->setProxyNamespace('Proxies'); $config->setAutoGenerateProxyClasses( $this->_application_mode == 'development' ); $dbconfs = Kohana::config('database'); foreach ( $dbconfs as $conn_name => $dbconf ) $this->_em[ $conn_name ] = EntityManager::create(array( 'dbname' => $dbconf['connection']['database'], 'user' => $dbconf['connection']['username'], 'password' => $dbconf['connection']['password'], 'host' => $dbconf['connection']['hostname'], 'driver' => 'pdo_mysql', ), $config); } } |
Once the module is enabled, you can access Doctrine from anywhere with the following code:
Doctrine::em() |
The module supports multiple databases and uses the Kohana database config file for connection details. To grab any specific database use the following (where connection_name is the name specified in the config file)
Doctrine::em('connection_name') |
Download the complete module here.
Hope this helps.
UPDATE 28 Dec 2010: This module is now defunct. Please instead use the Better Doctrine 2 Module for Kohana 3.