幾周前我爲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;
}
}
謝謝。我最終採用了類似的方法。 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