ZF2 - Doctrine2 MongoDB ODM - “在链配置命名空间中找不到该类”


ZF2 - Doctrine2 MongoDB ODM - "The class was not found in the chain configuration namespaces"

我经常在这里看到同样的错误,并且已经通读并根据之前的问题和答案进行了更改,但无济于事。

我已经通过Composer安装了Doctrine的Mongo ODM和Zend Framework,并完成了安装,如下所述

https://github.com/doctrine/DoctrineMongoODMModule

我已经修改了我的/config/autoload/module.doctrine-mongo-odm.local.php 文件,超出了上述文档的建议,以解决我的问题并基于此处类似问题的答案,因此它现在如下所示

<?php
return array(
    'doctrine' => array(
        'connection' => array(
            'odm_default' => array(
                'server'    => 'removed.mongolab.com',
                'port'      => '43957',
                'user'      => 'removed',
                'password'  => 'removed',
                'dbname'    => 'richard',
                'options'   => array()
            ),
        ),
        'configuration' => array(
            'odm_default' => array(
                'metadata_cache'     => 'array',
                'driver'             => 'odm_default',
                'generate_proxies'   => true,
                'proxy_dir'          => 'data/DoctrineMongoODMModule/Proxy',
                'proxy_namespace'    => 'DoctrineMongoODMModule'Proxy',
                'generate_hydrators' => true,
                'hydrator_dir'       => 'data/DoctrineMongoODMModule/Hydrator',
                'hydrator_namespace' => 'DoctrineMongoODMModule'Hydrator',
                'default_db'         => 'richard',
                'filters'            => array(),
                'logger'             => null
            )
        ),
        'odm_default' => array(
            'drivers' => array(
                'Application'Document' => 'odm_driver'
            )
        ),
        'odm_driver' => array(
            'class' => 'Doctrine'ODM'MongoDB'Mapping'Driver'AnnotationDriver',
            'cache' => 'array',
            'paths' => array(
                'module/Application/src/Application/Document'
            ),
        ),
        'documentmanager' => array(
            'odm_default' => array(
                'connection'    => 'odm_default',
                'configuration' => 'odm_default',
                'eventmanager' => 'odm_default'
            )
        ),
        'eventmanager' => array(
            'odm_default' => array(
                'subscribers' => array()
            )
        ),
    ),
);

我有一个文件/模块/应用程序/src/应用程序/文档/用户.php如下所示

<?php
namespace Application'Document;
use Doctrine'ODM'MongoDB'Mapping'Annotations;
class User {
    /** @ODM'Id */
    private $id;
    /** @ODM'Field(type="bin_data_timestamp") */
    private $timestamp;
    /** @ODM'Field(type="string") */
    private $username;
    /** @ODM'Field(type="bin_data_md5") */
    private $password;
    /** @ODM'Field(type="bin_data_uuid") */
    private $salt;
    /** @ODM'Field(type="string") */
    private $realName;
    /** @ODM'Field(type="string") */
    private $email;
    public function getId() {
        return $this->id;
    }
    // ...
    public function setId($id) {
        $this->id = $id;
    }
    // ...
}

在我的控制器中,我使用以下代码。

$dm = $this->getServiceLocator()->get('doctrine.documentmanager.odm_default');
$user = new User;
$user->setUsername("test");
$dm->persist($user);
$dm->flush();

但是,我遇到了臭名昭著的错误

The class 'Application'Document'User' was not found in the chain configured namespaces 

任何协助将不胜感激。

事实证明,我的配置变得有点混乱。我能够使用以下配置代码更正问题。

    'driver' => array(
        'odm_driver' => array(
            'class' => 'Doctrine'ODM'MongoDB'Mapping'Driver'AnnotationDriver',
            'paths' => array(__DIR__ . '/../../module/Application/src/Application/Document')
        ),
        'odm_default' => array(
            'drivers' => array(
                'Application'Document' => 'odm_driver'
            )
        ),
    ),

注意:应用程序''文档''用户包含的代码也会产生错误,使用以下代码更正。

namespace Application'Document;
use Doctrine'ODM'MongoDB'Mapping'Annotations as ODM;
/** @ODM'Document */
class User {
    // ...
}