views:

153

answers:

1

i seem to managed to integrate Doctrine 2's autoloaders to Zend's, tho i am not sure if i am doing it correctly ...

directory structure

/application
    /models
        /User.php   // following classes are doctrine models
        /Post.php
        /Tag.php
    /proxies
        /...        // proxy classes generated by doctrine
    /...            // other zend classes

bootstrap.php > _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;

1 question i have is can i use relative paths in newDefaultAnnotationDriver() and setProxyDir() isit safer to say that i just build the full path using realpath() and __DIR__ will be the safest?

i also wonder if doctrine requires me to setup autoloading 1 for each namespace, then push that autoloader?

then i wonder if the example here is workable? basically he used something like ...

protected function _initDoctrine()
{
    // Create the doctrine autoloader and remove it from the spl autoload stack (it adds itself)
    require_once 'Doctrine/Common/ClassLoader.php';
    $doctrineAutoloader = array(new \Doctrine\Common\ClassLoader(), 'loadClass');
    spl_autoload_unregister($doctrineAutoloader);

    // Fetch the global Zend Autoloader
    $autoloader = Zend_Loader_Autoloader::getInstance();

    // Push the doctrine autoloader to load for the Doctrine\ namespace
    $autoloader->pushAutoloader($doctrineAutoloader, 'Doctrine\\');
}

i wonder why he dont need to pass parameters into ClassLoader from the doctrine 2 sandbox, namespaces are passed into the autoloader, 1 by 1 and the autoloader registered. and why is spl_autoload_unregister($doctrineAutoloader) required?

A: 

ok i just say yes and close this

jiewmeng