这是将Zend Framework与Doctrine 2集成的正确方法吗?

时间:2010-07-30 15:22:51

标签: zend-framework doctrine

我似乎设法将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;

我有一个问题是,我可以使用newDefaultAnnotationDriver()setProxyDir()中的相对路径更安全地说我只是使用realpath()__DIR__构建完整路径最安全的?

我也想知道教义是否要求我为每个命名空间设置自动加载1,然后推送该自动加载器?

然后我想知道示例here是否可行?基本上他用的是像......

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\\');
}

我想知道为什么他不需要从doctrine 2沙箱中将参数传递到ClassLoader,命名空间被传递到自动加载器,1比1并且自动加载器已注册。为什么需要spl_autoload_unregister($doctrineAutoloader)

1 个答案:

答案 0 :(得分:3)

好吧,我只是说是,然后关闭这个