Invenzzia »

Using OPC and OPL with Doctrine ORM

From Invenzzia wiki

Jump to: navigation, search

The OPL loader is a powerful generic autoloader for all the libraries that use the Item_Subitem_Class class naming convention. With Open Power Classes, you can extend it for Doctrine ORM library without the need to register the next massive autoloader.

Below, you can see a sample code that handles both OPL and Doctrine:

require_once(BASE_PATH.'lib/Opl/Base.php');
Opl_Loader::setHandleUnknownLibraries(false);
Opl_Loader::addLibrary('Doctrine', array('basePath' => BASE_PATH.'lib/', 'handler' => null));
Opl_Loader::addLibrary('Opl', array('directory' => BASE_PATH.'lib/Opl/'));
Opl_Loader::addLibrary('Opt', array('directory' => BASE_PATH.'lib/Opt/'));
Opl_Loader::addLibrary('Opc', array('directory' => BASE_PATH.'lib/Opc/'));
Opl_Loader::register();
 
// Set up the model autoloader
Opc_DoctrineModels::setPath(BASE_PATH.'models/');
Opc_DoctrineModels::register();

Contrary to the ordinary PHP autoloader stack, the OPL autoloader performs a small library selection, using the first part of the class name. However, we must turn off handling unregistered libraries in order to load models. This is done by Opc_DoctrineModels class, registered later. It loads the model classes only. All we have to do is to specify the path to the model files.

A sample directory structure can be found below:

lib/
lib/Doctrine
lib/Opl
lib/Opc
lib/Opt
lib/Doctrine.php
models/
 ...

Because of changes in Doctrine 1.2 there is no need to have `lib/Doctrine.php` file. Instead use Doctrine_Core.

Personal tools