Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework 2 + Doctrine ODM, "The Class was not found in the chain configured namespaces" error?

When setting up ZF2 + ODM, I'm getting the following error:

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

The current setup is as the following:

ZF2 stable, installed doctrine ODM via composer.phar with content of 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 loaded

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

hydrator and proxy dirs are created

$ 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

the odm config looks like

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

I'm trying to use the following mapping

<?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;
    }

}

but calling it via

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

Any pointers?

like image 434
Wisu Suntoyo Avatar asked Sep 22 '12 02:09

Wisu Suntoyo


2 Answers

Real solution is not adding module.doctrine-mongo-odm.local.php into autoload directory, this lines worked for me as configuration

    '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'
            )
        ),
    ),
like image 164
siesta Avatar answered Sep 29 '22 21:09

siesta


This installation works fine with the current versions: ZF2, MongoDB, and Doctrine installation

Copy the odm’s default config file to our config directory. You will then need to modify the module.doctrine-mongo-odm.local.php as to your sever specifications. This is the configuration file where you set your server hosts, ports, username, and passwords. For example purposes we will assume everything is running on the same local machine and not make any modifications.

This is a Application module.config.php that will work for a hybrid solution ORM / ODM:

'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'
            )
        )                   
    )
)  
like image 41
webDEVILopers Avatar answered Sep 29 '22 21:09

webDEVILopers