简体   繁体   English

将Doctrine与Zend Framework 1.8应用程序集成

[英]Integrate Doctrine with Zend Framework 1.8 app

I'm interested in using Doctrine as an ORM for a new Zend Framework app I'm writing. 我有兴趣使用Doctrine作为我正在编写的新Zend Framework应用程序的ORM。 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. 我发现的每个例子都不同,其中很多都是ZF 1.8中新的自动加载功能的最新版本。 None of them have worked for me yet. 他们都没有为我工作过。

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. 我倾向于将它放在我的bootstrap文件中,但有些人建议制作一个Zend_Application_Resource插件。 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自动加载约定。

Any thoughts? 有什么想法吗? Thanks. 谢谢。

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

It is fully configurable via the Bootstrap resource configurations (example included, too). 它可以通过Bootstrap资源配置完全配置(也包括示例)。 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. 不幸的是,Doctrine在模型文件夹中搜索具有与文件名相同的类名的模型(与ZF命名方案不匹配),因此实际上无法摆脱注册Doctrine Autoloader。 The resource Loader looks like this: 资源Loader看起来像这样:

<?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;
    }
}

http://weierophinney.net/matthew/archives/220-Autoloading-Doctrine-and-Doctrine-entities-from-Zend-Framework.html http://weierophinney.net/matthew/archives/220-Autoloading-Doctrine-and-Doctrine-entities-from-Zend-Framework.html

take a look at this post. 看看这篇文章。 It gives a detailed explanation, directory structure and how to use autaloading features. 它提供了详细的说明,目录结构以及如何使用自动编组功能。

As far as auto-loading is concerned, you can actually use the Doctrine loader with the new Zend_Loader_Autoloader stack quite easily. 就自动加载而言,您可以非常轻松地将Doctrine加载器与新的Zend_Loader_Autoloader堆栈一起使用。 Take a look at this page , especially where it mentions the pushAutoloader() method. 看一下这个页面 ,特别是它提到了pushAutoloader()方法。

Here's the basic run down, though: 不过,这是基本的运行:

$autoloader = Zend_Loader_Autoloader->getInstance();
$autoloader->pushAutoloader(array('Doctrine', 'autoload'), 'Doctrine');

This will use Doctrine's own autoloader for only classes that begin with Doctrine, if they are not already found by other autoloaders in the stack. 这将使用Doctrine自己的自动加载器,仅用于以Doctrine开头的类,如果堆栈中的其他自动加载器尚未找到它们。

Hope this helps a bit. 希望这个对你有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM