Class 'MongoId' not found (Zend Framework with MongoDB Doctrine)
Asked Answered
N

1

4

Im' currently trying to integrate MongoDB with Doctrine in ZendFramework. I did a lot of tutorial (on StackOverflow or anywhere else) but nothing is really working.

I followed step by step a tutorial: http://www.bigwisu.com/2012/10/03/zend-framework-2-doctrine-odm and I got an error that I don't understand.

Fatal error: Class 'MongoId' not found in /home/prooxi/www/zframework/vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/Types/IdType.php on line 38

The IdType.php is source code for the mongoDB, so the error must be somewhere else. Here is the files I have. (Admin is the name of the module)

config/application.config.php

<?php
return array(
    'modules' => array(
               'Application',
               'DoctrineModule',
               'DoctrineMongoODMModule',
               'Udmin',
               'Listing',
               'Admin',
    ),

    'module_listener_options' => array(
        'module_paths' => array(
            './module',
            './vendor',
        ),

        'config_glob_paths' => array(
            'config/autoload/{,*.}{global,local}.php',
        ),

    ),

);

config/autoload/module.doctrine-mongo-odm.local.php

<?php
return array(
         'doctrine' => array(

                 'connection' => array(
                               'odm_default' => array(
                                          'server'           => 'MYDBADRESS',
                                          'port'             => '27017',
                                          /* 'connectionString' => null, */
                                          /* 'user'             => null, */
                                          /* 'password'         => null, */
                                          'dbname'           => 'px_boutique_test27',
                                          'options'          => array()
                                          ),
                               ),

                 'configuration' => array(
                              'odm_default' => array(
                                         'metadata_cache'     => 'array',
                                         'driver'             => 'odm_default',
                                         'generate_proxies'   => true,
                                         'proxy_dir'          => 'data/DoctrineMongoODMModule/Proxy',
                                         /* 'proxy_dir'          => __DIR__ . '/module/Admin/src/Admin/Model/Proxy',  */
                                         /* 'proxy_dir'          => __DIR__ . '/module/Udmin/src/Udmin/Model/Proxy', */
                                         'proxy_namespace'    => 'DoctrineMongoODMModule\Proxy',
                                         /* 'proxy_namespace'    => 'Udmin\Model\Proxy', */
                                         'generate_hydrators' => true,
                                         'hydrator_dir'       => 'data/DoctrineMongoODMModule/Hydrator',
                                         /* 'hydrator_dir'       => __DIR__ . '/module/Udmin/src/Udmin/Model/Hydrator', */
                                         'hydrator_namespace' => 'DoctrineMongoODMModule\Hydrator',
                                         /* 'hydrator_namespace' => 'Udmin\Model\Hydrator', */
                                         'default_db'         => 'test27',
                                         'filters'            => array(),  // array('filterName' => 'BSON\Filter\Class'),
                                         /* 'logger'             => null // 'DoctrineMongoODMModule\Logging\DebugStack' */
                                         )
                              ),

                 'driver' => array(
                           'odm_default' => array(
                                      'drivers' => array(
                                                 'Admin\Document' => 'aplikasi'
                                                 )
                                      ),
                           'aplikasi' => array(
                                       'class' => 'Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver',
                                       'cache' => 'array',
                                       'paths' => array(
                                            'module/Admin/src/Admin/Document'
                                            )
                                       )
                           ),
                 'documentmanager' => array(
                                'odm_default' => array(
                                           'connection'    => 'odm_default',
                                           'configuration' => 'odm_default',
                                           'eventmanager' => 'odm_default'
                                           )
                                ),
                 'eventmanager' => array(
                             'odm_default' => array(
                                        'subscribers' => array()
                                        )
                             ),              

                 ),
         );

Module/Admin/Src/Admin/Controller/AdminController.php

<?php
namespace Admin\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Mongo;
use Zend\Session\SaveHandler\MongoDB;
use Zend\Session\SaveHandler\MongoDBOptions;
use Zend\Session\SessionManager;
use Admin\Document\Boutique;

 class AdminController extends AbstractActionController
 {
     public function indexAction()
     {
       $dm = $this->getServiceLocator()->get('doctrine.documentmanager.odm_default');
       $b = new Boutique();

       /* $dm->getRepository('Admin\Document\Boutique')->findAll(); */
       $dm->find('Admin\Document\Boutique', '52e6c677362dca7fcd40ab09');
     }
}

Module/Admin/config/module.config.php

<?php
return array(
         'controllers' => array(
                    'invokables' => array(
                              'Admin\Controller\Admin' => 'Admin\Controller\AdminController',
                              ),
                    ),

         'router' => array(
                   'routes' => array(
                         'admin' => array(
                                  'type'    => 'segment',
                                  'options' => array(
                                             'route'    => '/admin[/][:action][/:id]',
                                             'constraints' => array(
                                                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                                                        'id'     => '[0-9]+',
                                                        ),
                                             'defaults' => array(
                                                     'controller' => 'Admin\Controller\Admin',
                                                     'action'     => 'index',
                                                     ),
                                             ),
                                  ),
                         ),
                   ),

         'view_manager' => array(
                     'template_path_stack' => array(
                                    'admin' => __DIR__ . '/../view',
                                    ),
                     ),

         );

The purpose of the module is to connect to an existing MongoDB databate and just make a list of all the document in it.

Thank you !

Gilles

Naive answered 22/2, 2014 at 11:1 Comment(10)
Have you made sure that the Mongo driver for PHP has been installed?Nesselrode
Yap, the mongo driver is installed !Naive
Have you either namespaced the class using a use statement or prefixed the class with a backslash; I.E new \MongoId($id)?Brow
There is already "namespace Doctrine\ODM\MongoDB\Types" in IDType.php. Do I still need to write a "use" ? And is the error really in IdType ? Because it's a source code, it should work (there is no error like this on the internet). It would be weird.Naive
You would need to use \MongoId to access it from that file.Nesselrode
It's already done. In the source code, the error line is on "$value = new \MongoId($value);"Naive
I'm sorry @PhillipWhelan, it's seems you were right the first time. The MongoDB Driver is installed on my server and there is "extention=mongo.so" in the php.ini file, but when I phpinfo(), no Mongo driver appears. This certainly is the error.Naive
Hopefully you solved it.Nesselrode
I didn't really solved it, but I will search. If I don't find, I will certainly close this question and start another one. thanks @PhillipWhelan !Naive
thanks @AlexP, you solved my problem :pGusman
K
4

If you are using the newer mongodb extension rather than mongo extension for PHP you will need to use MongoDB\BSON\ObjectID as detailed on the php.net page

Knurly answered 8/8, 2016 at 14:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.