2011-05-18 69 views
0

我試圖自動加載一些來自Doctrine MongoDB實現的文檔,但我不知道如何。根據該網站的說法,資源自動加載器將其視爲假定您的名稱空間將使用下劃線(_)而不是新名稱空間。使用PHP 5.3命名空間的Zend資源自動加載

當我想運行此行的例子:無需使用需要

if(!$this->dm->getRepository('Documents\User')->findOneBy(array('email'=>$userInfo['email']))){ 
     $user = new User(); 
//... 

。然而,現在當我嘗試在bootstrap中使用資源加載器時,它告訴我它無法加載php文件。

存儲庫是存儲庫\用戶命名空間和用戶()是在文獻\用戶命名空間。

這是我的引導

$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH.'/models/documents/', 
'namespace' => 'Documents', 

));

有沒有辦法做到這一點,還是我停留在我的模型中使用require_once?

感謝,

-Zend菜鳥

回答

1

在Zend autloader是不會削減它的命名空間5.3代碼。

最簡單的事情是推動學說的類加載器到了Zend之一。

使用這樣的事情在你的引導,而不是上面的代碼。在這個例子中,我假設DocumentRepository文件夾中APPLICATION_PATH . '/models'

protected function _initAutoloader() 
{ 
    $autoloader = Zend_Loader_Autoloader::getInstance(); 

    require_once 'Doctrine/Common/ClassLoader.php'; 

    $documentAutoloader = new \Doctrine\Common\ClassLoader('Document', APPLICATION_PATH . '/models'); 
    $autoloader->pushAutoloader(array($documentAutoloader, 'loadClass'), 'Document'); 

    $repositoryAutoloader = new \Doctrine\Common\ClassLoader('Repository', APPLICATION_PATH . '/models'); 
    $autoloader->pushAutoloader(array($repositoryAutoloader, 'loadClass'), 'Repository'); 

    return $autoloader; 
} 
+0

aweeesome。我會嘗試,當我今天下車,但看起來正是我一直在尋找。 – 2011-05-18 16:52:54

4

你可以使用馬修緯二路O'Phinneys Backported ZF2 Autoloaders

特點

  • PSR-0兼容include_path自動加載
  • PSR-0兼容每個前綴或命名空間的自動加載
  • 類映射自動加載,包括類映射生成
  • 自動裝載機工廠用於一次

加載多個自動加載機的策略實施例

protected function _initAutoloader() 
{ 
    require_once 'path/to/library/ZendX/Loader/StandardAutoloader.php'; 
    $loader = new ZendX_Loader_StandardAutoloader(array(
     'namespaces' => array(
      'Repository' => APPLICATION_PATH . '/models/', 
      'Document' => APPLICATION_PATH . '/models/', 
     ), 
    )); 
    $loader->register(); // register with spl_autoload_register() 
}