我正在尝试从Doctrine MongoDB实现中自动加载一些文档,但我不确定如何。根据该网站,资源自动加载器认为您的命名空间将使用下划线 (_) 而不是新的命名空间。
作为一个例子,我想运行这一行:
if(!$this->dm->getRepository('Documents'User')->findOneBy(array('email'=>$userInfo['email']))){
$user = new User();
//...
无需使用要求。但是,现在当我尝试在引导中使用资源加载器时,它告诉我它无法加载 php 文件。
存储库是存储库''用户命名空间,User() 位于文档''用户命名空间中。
这是我的引导
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH.'/models/documents/',
'namespace' => 'Documents',
));
有没有办法做到这一点,或者我是否在我的模型中使用require_once?
谢谢
-赞德菜鸟
您可以使用Matthew Weier O'Phinneys 向后移植的ZF2自动加载机
特征
- 符合 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()
}
Zend autloader 不会为 5.3 命名空间代码削减它。
最简单的方法是将 Doctrine 的类加载器推入 Zend 加载器。
在你的引导中使用类似的东西,而不是上面的代码。在此示例中,我假设Document
和Repository
文件夹位于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;
}