2009-06-10 79 views
4

我有興趣使用Doctrine作爲我正在編寫的新Zend Framework應用的ORM。我試圖找出儘可能直接整合它的最佳方法。我找到的每個例子都不同,它們中的很多都是ZF 1.8中的新自動加載功能的前期日期。他們中沒有人爲我工作。Zend Framework 1.8集成原則應用

有沒有人有一個很好的方法來做到這一點?我傾向於將它放在我的引導文件中,但有人建議製作一個Zend_Application_Resource插件。困難的部分似乎是讓負載路徑對Doctrine命名空間和默認不遵循Zend自動加載約定的模型類都正確地工作。

有什麼想法?謝謝。

回答

3

幾周前我爲Doctrine和Zend Framework編寫了一個Resource Bootstrapper,並將它全部轉化爲一個小包裝框架,因爲我認爲ZF和Doctrine是一個很棒的團隊。 您可以在這裏閱讀文章: http://coffeecoders.de/2009/06/using-the-zend-framework-18-bootstrapper-and-doctrine-110/

它可以通過Bootstrap資源配置完全配置(也包括示例)。不幸的是,Doctrine在模型文件夾中搜索具有與文件名相同的類名(與ZF命名方案不匹配)的模型,所以實際上不可能擺脫註冊Doctrine Autoloader。 資源加載器看起來是這樣的:

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

謝謝。我最終採用了類似的方法。 Matthew Lurz爲ZendX資源插件編寫了[提案](http://framework.zend.com/wiki/display/ZFPROP/Zend_Application_Resource_Doctrine+-+Matthew + Luz)。他粘貼了一些最初的[規範](http://pastie.org/481635)和[code](http://pastie.org/481633)。這與你所擁有的相似,並解決了我的問題。 – 2009-06-11 00:01:49

0

就自動加載而言,您可以很容易地使用帶有新Zend_Loader_Autoloader堆棧的Doctrine加載器。看看this page,尤其是它提到pushAutoloader()方法的地方。

這裏是基本的跑下來,雖然:

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

這將使用Doctrine自己的自動加載磁帶機僅與學說開始,如果它們尚未通過堆棧中其它自動加載機中發現的類。

希望這會有所幫助。