0

I'm using http://docs.php.net/manual/en/mongodb.tutorial.library.php and when I use the example they provide, I get a PHP Fatal error: Class 'MongoDB\Collection' not found. I'm using composer and even without the require mongodb\mongodb I get and a MongoDB\Driver\Manager Object if I print the $manager suggesting that it is using one MongoDB that it's not the one that I've installed (because it is not installed, at least via composer).

Any idea to find that MongoDB or to solve my problem?

SOLVED: Because I'm using this from command line and not from Luracast/Restler, I need to require the autoload on the construct during the command line

namespace NES\Utils;

use MongoDB;

class Mongo
{

    private $manager;
    private $collection;
    public $result;

    function __construct($collection)
    {
        require_once "../../vendor/autoload.php";

        $this->manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
        $this->collection = new MongoDB\Collection($this->manager,"nestories.queue");
        $result = $this->collection->find( [ 'teste' => 'teste'] );

        print_r($result);
        }
}

Regards

6
  • Are you include/requiring the composer's autoload.php? Commented Jan 19, 2016 at 3:18
  • I'm using Luracast\Restler and everything works fine with another requires. Commented Jan 19, 2016 at 3:25
  • Again, are you include/requiring the composer's autoload.php? Can you share your code? Commented Jan 19, 2016 at 3:25
  • Se my update on the main post please. Commented Jan 19, 2016 at 3:43
  • Your second example (the one you say isn't working) doesn't include the composer autoloader. You need it to use composer dependencies. Also, MongoDB isn't a class on its own by the look of it, so use MongoDB; is probably causing issues. Commented Jan 19, 2016 at 3:46

1 Answer 1

1

Are you using the composer autoload in your project? Also, are you working with namespaces? An alternative to use the Composer mongo package is to install the PECL extension for Mongo: https://pecl.php.net/package/mongo

You will need to make sure that it's installed in each environment if you use the PECL extension.

The error that you're experiencing is most likely because you aren't correctly including the vendor autoload.php file.

<?php
require 'vendor/autoload.php';

What does your error log output say when you try to include the file? If you're using xdebug it will dump a lot of content, but generally you should get a failed to include file '/path/to/incorrect/file/name.php' error.

Sign up to request clarification or add additional context in comments.

1 Comment

Happy to have helped. Most devs will run into this problem quite a few times.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.