Zend Framework 2 + Doctrine ODM,“在链配置的命名空间中找不到类”错误?

时间:2012-09-22 02:34:13

标签: mongodb doctrine-orm zend-framework2 doctrine-odm odm

设置ZF2 + ODM时,我收到以下错误:

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

当前设置如下:

ZF2稳定,通过composer.phar安装了学说ODM,内容为composer.json

{
    "name": "zendframework/skeleton-application",
    "description": "Skeleton Application for ZF2",
    "license": "BSD-3-Clause",
    "keywords": [
        "framework",
        "zf2"
    ],
    "homepage": "http://framework.zend.com/",
    "minimum-stability": "dev",
    "require": {
        "php": ">=5.3.3",
        "zendframework/zendframework": "2.0.0",
        "doctrine/doctrine-mongo-odm-module": "dev-master"
    }
}

加载模块

'modules' => array(
    'Application',
    'DoctrineModule',
    'DoctrineMongoODMModule',
),

创建水化器和代理dirs

$ ls -l data/DoctrineMongoODMModule/
total 0
drwxrwxrwx  2 wisu  staff  68 Sep 12 08:34 Hydrators
drwxrwxrwx  2 wisu  staff  68 Sep 12 08:35 Proxy

odm配置看起来像

'driver' => array(
    'odm_default' => array(
        'drivers' => array(
            'Application\Document' => 'aplikasi'
        )
    ),
    'aplikasi' => array(
        'class' => 'Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver',
        'cache' => 'array',
        'paths' => array(
            'module/Application/src/Application/Document'
        )
    )
),

我正在尝试使用以下映射

<?php

namespace Application\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/** @ODM\Document(collection="user") */
class User
{
    /** @ODM\Id */
    private $id;

    /** @ODM\Field(type="string") */
    private $name;

    /**
     * @return the $id
     */
    public function getId() {
        return $this->id;
    }

    /**
     * @return the $name
     */
    public function getName() {
        return $this->name;
    }

    /**
     * @param field_type $id
     */
    public function setId($id) {
        $this->id = $id;
    }

    /**
     * @param field_type $name
     */
    public function setName($name) {
        $this->name = $name;
    }

}

但是通过

调用它
<?php

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Document\User;

class IndexController extends AbstractActionController
{
    public function indexAction()
    {

        $dm = $this->getServiceLocator()->get('doctrine.documentmanager.odm_default');

        $user = new User();
        $user->setName("Gembul");

        $dm->persist($user);
        $dm->flush();

        return new ViewModel();
    }
}

任何指针?

3 个答案:

答案 0 :(得分:3)

真正的解决方案是不将module.doctrine-mongo-odm.local.php添加到自动加载目录中,这些行对我来说是配置

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

答案 1 :(得分:3)

此安装适用于当前版本: ZF2, MongoDB, and Doctrine installation

将odm的默认配置文件复制到config目录。然后,您需要修改module.doctrine-mongo-odm.local.php以了解您的服务器规范。这是您配置服务器主机,端口,用户名和密码的配置文件。出于示例目的,我们将假设所有内容都在同一本地计算机上运行,​​而不进行任何修改。

这是一个适用于混合解决方案ORM / ODM的应用程序 module.config.php

'doctrine' => array(
    'driver' => array(
        'orm_driver' => array(
            'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
            'cache' => 'array',
            'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity')
        ),
        'orm_default' => array(
            'drivers' => array(
                __NAMESPACE__ . '\Entity' => 'orm_driver'
            )
        ),
        'odm_driver' => array(
            'class' => 'Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver',          
            'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Document')
        ),
        'odm_default' => array(
            'drivers' => array(
                __NAMESPACE__ . '\Document' => 'odm_driver'
            )
        )                   
    )
)  

答案 2 :(得分:0)

发现问题......

配置文件module.doctrine-mongo-odm.local.php不在自动加载目录下...