5

设置 ZF2 + ODM 时,出现以下错误:

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

当前设置如下:

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

{
    "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',
),

创建 hydrator 和代理目录

$ 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();
    }
}

任何指针?

4

3 回答 3

3

此安装适用于当前版本: ZF2、MongoDB 和 Doctrine 安装

将 odm 的默认配置文件复制到我们的配置目录。然后,您需要根据您的服务器规范修改 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'
            )
        )                   
    )
)  
于 2013-01-09T10:01:51.477 回答
3

真正的解决方案是不将 module.doctrine-mongo-odm.local.php 添加到 autoload 目录中,这些行作为配置对我有用

    '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'
            )
        ),
    ),
于 2012-11-21T22:51:13.353 回答
0

发现问题了...

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

于 2012-09-24T09:37:46.563 回答