user1504736
user1504736

Reputation: 13

php MongoDB\Driver\Manager not found when executing Symfony command

Ok, so I have a project my mongo connection works fine when viewing pages etc. But when I try to load the manage class in command (ContainerAwareCommand) I get class not found exception:

[Symfony\Component\Debug\Exception\ClassNotFoundException]
Attempted to load class "Manager" from namespace "MongoDB\Driver".
Did you forget a "use" statement for another namespace?

I'm using symfony 3.2

Here's what I do:

class TestCommand extends ContainerAwareCommand {
   protected function configure() {
      $this->setName('testcommand:test')
        ->setDescription('Test...')
        ->setHelp('Test...');
   }

 protected function execute(InputInterface $input, OutputInterface   $output) {
    $db = new Manager("mongodb://localhost:27017");
    $output->writeln([
        'Test',
        '========================',
        '',
    ]);
  }
 }

I import the driver with:

use MongoDB\Driver\Manager;

PhpStorm recognizes the import.

Any ideas what stops the class from loading? Once again, it loads normally when I navigate my pages etc.

Thanks!

UPDATE (As requested) !

Registered a service:

services:
  test.service:
    class: AppBundle\Test\TestService
    public: true

The service:

namespace AppBundle\Test;


use MongoDB\Driver\Manager;

class TestService {

   public function foo() {
      $manager = new Manager("mongodb://localhost:27017");
   }
}

added this to execute method:

$ts = $this->getContainer()->get("test.service");
$ts->foo();

result:

[Symfony\Component\Debug\Exception\ClassNotFoundException]
Attempted to load class "Manager" from namespace "MongoDB\Driver".
Did you forget a "use" statement for another namespace?

What am I missing?

ANOTHER UPDATE

I also noticed that I couldn't switch to production because of the same, but more general, error.

Turns out I can't use namespaces but can get the class (Manager) via fully qualified name at RUN TIME ONLY!!!

Upvotes: 1

Views: 1607

Answers (1)

Christian Felix
Christian Felix

Reputation: 673

I had had the same problem and was somewhat distracted by the error message, this problem occurs while executing the script in the CLI context where not all extension has been properly installed.

Check whether all your extension, escpecially for MongoDb are installed.

Upvotes: 1

Related Questions