Return to Snippet

Revision: 30063
at August 6, 2010 21:14 by jiewmeng


Initial Code
function _initDoctrine() {
	// setup Zend & Doctrine Autoloaders
	require_once "Doctrine/Common/ClassLoader.php";

	$zendAutoloader = Zend_Loader_Autoloader::getInstance();

	// $autoloader = array(new \Doctrine\Common\ClassLoader(), 'loadClass');

	$autoloader = array(new \Doctrine\Common\ClassLoader('Symfony'), 'loadClass');
	$zendAutoloader->pushAutoloader($autoloader, 'Symfony\\');
	$autoloader = array(new \Doctrine\Common\ClassLoader('Doctrine'), 'loadClass');
	$zendAutoloader->pushAutoloader($autoloader, 'Doctrine\\');
	$autoloader = array(new \Doctrine\Common\ClassLoader('DoctrineExtensions'), 'loadClass');
	$zendAutoloader->pushAutoloader($autoloader, 'DoctrineExtensions\\');
	$autoloader = array(new \Doctrine\Common\ClassLoader('Application\\Models', realpath(__DIR__ . '/..')), 'loadClass');
	$zendAutoloader->pushAutoloader($autoloader, 'Application\\Models\\');
	$autoloader = array(new \Doctrine\Common\ClassLoader('Application\\Proxies', realpath(__DIR__ . '/..')), 'loadClass');
	$zendAutoloader->pushAutoloader($autoloader, 'Application\\Proxies');
	$autoloader = array(new \Doctrine\Common\ClassLoader('DoctrineExtensions'), 'loadClass');
	$zendAutoloader->pushAutoloader($autoloader, 'DoctrineExtensions\\');

	// setup configuration as seen from the sandbox application
	// TODO: read configuration from application.ini
	$config = new \Doctrine\ORM\Configuration;
	$cache = new \Doctrine\Common\Cache\ArrayCache;
	$config->setMetadataCacheImpl($cache);
	$driverImpl = $config->newDefaultAnnotationDriver(realpath(__DIR__ . '/models'));
	$config->setMetadataDriverImpl($driverImpl);
	$config->setQueryCacheImpl($cache);
	$config->setProxyDir(realpath(__DIR__ . '/proxies'));
	$config->setProxyNamespace('Application\\Proxies');
	$config->setAutoGenerateProxyClasses(true);

	$connectionOptions = array(
	  'driver' => 'pdo_mysql',
	  'user' => 'root',
	  'password' => '',
	  'dbname' => 'learningzf'
	);

	// setup entity manager
	$em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config);
	Zend_Registry::set("em", $em);
	return $em;
}

Initial URL

                                

Initial Description
an alternative to a previous method i did using Zend Framework's autoloaders only. this method uses Doctrine's autoloaders for doctrine's classes and Zend's for Zend's :) 

i think the difficulty i had was autoloading from a specific directory (with just Zend's autoloaders). eg. in Doctrine autoloaders i will do

    new Doctrine\Common\ClassLoader(
	    'Application\Models', 
		APPLICATION_PATH . '/models'
	);
	
where the 2nd param is the path to find the classes. basically, 

- we create a resource method in `bootstrap.php`. 
- in it, we setup doctrine in a similar way to how the doctrine sandbox sets it up. to setup autoloading of doctrine's classes, as we create new doctrine class loaders, we push them into the zend's autoloader.
- then its basically the same as the sandbox app, setting up caches, metadata drivers, proxies etc.

Initial Title
Integrating Zend Framework 1.10 with Doctrine 2 (using Doctrine's Autoloader)

Initial Tags

                                

Initial Language
PHP