将 Doctrine 与 Zend Framework 1.8 应用程序集成 [英] Integrate Doctrine with Zend Framework 1.8 app

查看:18
本文介绍了将 Doctrine 与 Zend Framework 1.8 应用程序集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有兴趣使用 Doctrine 作为我正在编写的新 Zend Framework 应用程序的 ORM.我试图找出尽可能简单地集成它的最佳方法.我发现的每个示例都不同,其中很多示例早于 ZF 1.8 中的新自动加载功能.他们都没有为我工作过.

I'm interested in using Doctrine as an ORM for a new Zend Framework app I'm writing. I'm trying to figure out the best way to integrate it as straightforward as possible. Every example I find is different, and a lot of them pre-date the new autoloading features in ZF 1.8. None of them have worked for me yet.

有没有人有这样做的好方法?我倾向于将它放在我的引导文件中,但有些人建议制作 Zend_Application_Resource 插件.困难的部分似乎是让 Doctrine 命名空间和模型类的加载路径正常工作,默认情况下不遵循 Zend 自动加载约定.

Does anyone have a good way to do this? I'm inclined to want to place it in my bootstrap file, but some people suggest making a Zend_Application_Resource plugin. The hard part seems to be getting the load paths working correctly for both the Doctrine namespace and the model classes which by default don't follow the Zend auto-loading convention.

有什么想法吗?谢谢.

推荐答案

几周前我为 Doctrine 和 Zend Framework 编写了一个 Resource Bootstrapper,并把它全部变成了一个小的包装框架,因为我认为 ZF 和 Doctrine 是一个很棒的团队.你可以在这里读这篇文章:http://coffeecoders.de/2009/06/using-the-zend-framework-18-bootstrapper-and-doctrine-110/

I wrote a Resource Bootstrapper for Doctrine and Zend Framework a few weeks ago and turned it all into a small wrapper framework, cause I think ZF and Doctrine are a great team. You can read the article here: http://coffeecoders.de/2009/06/using-the-zend-framework-18-bootstrapper-and-doctrine-110/

它可以通过 Bootstrap 资源配置完全配置(也包括示例).不幸的是,Doctrine 在模型文件夹中搜索与文件名具有相同类名的模型(与 ZF 命名方案不匹配),因此实际上无法摆脱注册 Doctrine Autoloader.资源加载器如下所示:

It is fully configurable via the Bootstrap resource configurations (example included, too). Unfortunately Doctrine searches for Models in the model folder with the same classname as the filename (which doesn't match the ZF naming scheme) so it was actually not possible to get rid of registering the Doctrine Autoloader. The resource Loader looks like this:

<?php
/**
 * Doctrine model loading bootstrap resource. Options must provide a connection string.
 * directory option for model directory is optional (default is ./models).
 * Further options will be set for the Doctrine manager via setAttribute (e.g. model_loading). 
 * @author daff
 */
class Cuckoo_Application_Resource_Model extends Zend_Application_Resource_ResourceAbstract
{
    public function init()
    {
        $manager = Doctrine_Manager::getInstance();
        $options = $this->getOptions();

        foreach($options as $key => $value)
        {
           if($key != 'connection' && $key != 'directory')
                    $manager->setAttribute($key, $value);
        }

        if(empty($options['connection']))
            throw new Exception("No database connection string provided!");
        Doctrine_Manager::connection($options['connection']);
        if(empty($options['directory']))
            $dir = './models';
        else
            $dir = $options['directory'];
        Doctrine::loadModels(realpath($dir));
        return $manager;
    }
}

这篇关于将 Doctrine 与 Zend Framework 1.8 应用程序集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆