2

我在我的项目中使用 Zend Framework 和 Doctrine 2。一切正常,除了自动加载器不从 Doctrine 存储库加载 Zend 类。

这是 Zend 自动加载器的引导部分:

/**
 * Register namespace Default_
 * @return Zend_Application_Module_Autoloader
 */
protected function _initAutoload() {
    $autoloader = new Zend_Application_Module_Autoloader(array(
        'namespace' => 'Default_',
        'basePath'  => dirname(__FILE__),
    ));
    return $autoloader;
}

这是我的 Doctrine 初始化引导部分:

/**
 * Initialize Doctrine
 * @return Doctrine_Manager
 */
public function _initDoctrine() {
    // include and register Doctrine's class loader
    require_once('Doctrine/Common/ClassLoader.php');
    $classLoader = new \Doctrine\Common\ClassLoader(
        'Doctrine', 
        APPLICATION_PATH . '/../library/'
    );
    $classLoader->register();

    $classLoader = new \Doctrine\Common\ClassLoader(
        'Repositories',
        APPLICATION_PATH . '/../library/Model'
    );
    $classLoader->register();

    // create the Doctrine configuration
    $config = new \Doctrine\ORM\Configuration();

    // setting the cache ( to ArrayCache. Take a look at
    // the Doctrine manual for different options ! )
    $cache = new \Doctrine\Common\Cache\ApcCache;
    $config->setMetadataCacheImpl($cache);
    $config->setQueryCacheImpl($cache);

    // choosing the driver for our database schema
    // we'll use annotations
    $driver = $config->newDefaultAnnotationDriver(
        APPLICATION_PATH . '/../library/Model'
    );
    $config->setMetadataDriverImpl($driver);

    // set the proxy dir and set some options
    $config->setProxyDir(APPLICATION_PATH . '/../library/Model/Proxies');
    $config->setAutoGenerateProxyClasses(true);
    $config->setProxyNamespace('Model\Proxies');

    // now create the entity manager and use the connection
    // settings we defined in our application.ini
    $connectionSettings = $this->getOption('doctrine');
    $conn = array(
        'driver'    => $connectionSettings['conn']['driv'],
        'user'      => $connectionSettings['conn']['user'],
        'password'  => $connectionSettings['conn']['pass'],
        'dbname'    => $connectionSettings['conn']['dbname'],
        'host'      => $connectionSettings['conn']['host']
    );
    $entityManager = \Doctrine\ORM\EntityManager::create($conn, $config);

    // push the entity manager into our registry for later use
    $registry = Zend_Registry::getInstance();
    $registry->entitymanager = $entityManager;

    return $entityManager;
}

我该如何解决这个问题?

4

3 回答 3

1

您的 _initAutoload 完全没有必要。只需添加

autoloadernamespaces[]  = Default
autoloadernamespaces[]  = Doctrine

到您的 application.ini

于 2012-07-13T16:51:04.080 回答
1

我同意 _initAutoload() 不应该是必要的,但我怀疑你会在 application.ini 中需要它:

autoloaderNamespaces[] = "Doctrine"
autoloaderNamespaces[] = "Model"
于 2012-07-17T18:48:08.093 回答
0

如果你刚刚开始你的项目,我会建议使用 zf2(beta5 最后一个 beta 跟随:http://framework.zend.com/zf2/blog/entry/Zend-Framework-2-0-0beta5-Released
是教程我用于我的项目 zf2 + 学说 2
http://www.jasongrimes.org/2012/01/using-doctrine-2-in-zend-framework-2/

于 2012-07-13T16:07:11.747 回答