Мне, кажется, удалось интегрировать автозагрузчики Doctrine 2 в Zend, хотя я не уверен, правильно ли я это делаю ...
структура каталогов
/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 у меня есть вопрос: могу ли я использовать относительные пути в newDefaultAnnotationDriver()
и setProxyDir()
, так что безопаснее сказать, что я просто строюполный путь с использованием realpath()
и __DIR__
будет самым безопасным?
Мне также интересно, если доктрина требует, чтобы я настроил автозагрузку 1 для каждого пространства имен, а затем нажмите этот автозагрузчик?
тогда мне интересно, работает ли пример здесь ?в основном он использовал что-то вроде ...
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\\');
}
Интересно, почему ему не нужно передавать параметры в ClassLoader
из песочницы doctrine 2, пространства имен передаются в автозагрузчик, 1 на 1, и автозагрузчик регистрируется,и почему spl_autoload_unregister($doctrineAutoloader)
требуется?