We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Service 'db' wasn't found in the dependency injection container

I have re-read many articles I haven't understood in what a problem, help. By call save() the mistake appears. I use the Mongo database.

#0 [internal function]: Phalcon\Di->get('db', NULL)
#1 [internal function]: Phalcon\Di->getShared('db')
#2 [internal function]: Phalcon\Mvc\Model\Manager->_getConnection(Object(Camers), NULL)
#3 [internal function]: Phalcon\Mvc\Model\Manager->getWriteConnection(Object(Camers))
#4 [internal function]: Phalcon\Mvc\Model->getWriteConnection()
#5 C:\xampp\htdocs\laravel\app\controllers\CamerController.php(14): Phalcon\Mvc\Model->save()
#6 [internal function]: CamerController->AddCamersAction()
#7 [internal function]: Phalcon\Dispatcher->callActionMethod(Object(CamerController), 'AddCamersAction', Array)
#8 [internal function]: Phalcon\Dispatcher->dispatch()
#9 C:\xampp\htdocs\laravel\public\index.php(31): Phalcon\Mvc\Application->handle()
#10 {main}

Have you set up your database connection? That's usually done in a services.php or bootstrap.php file included in index.php.

Phalcon has lots of MVC examples available here: https://github.com/phalcon/mvc

Check out the "Simple" example to start: https://github.com/phalcon/mvc/blob/master/simple/public/index.php



1.7k
edited Aug '18

Have you set up your database connection? That's usually done in a services.php or bootstrap.php file included in index.php.

Phalcon has lots of MVC examples available here: https://github.com/phalcon/mvc

Check out the "Simple" example to start: https://github.com/phalcon/mvc/blob/master/simple/public/index.php

services.php

 $di->setShared('config', function () {
     return include APP_PATH . "/config/config.php";
 });

 $di->set("mongo", function () {
     $config = $this->getConfig();

    $db_string = sprintf('mongodb://%s:%s', $config->mongodb->host, $config->mongodb->port);

     if (isset($config->mongodb->username) and isset($config->mongodb->password)) {
        $db_string = sprintf('mongodb://%s:%[email protected]%s:%s',
                $config->mongodb->username,
                 $config->mongodb->password,
                 $config->mongodb->host,
                 (string)$config->mongodb->port);
     }

     try {
         $mongo = new \Phalcon\Db\Adapter\MongoDB\Client($db_string);
         return $mongo->selectDatabase($config->mongodb->database);
     } catch (MongoConnectionException $e) {
         die('Failed to connect to MongoDB '.$e->getMessage());
    }
 }, true
 );

config.php

>   'mongodb' => [
>         'host' => 'localhost',
>         'port' => '27017',
>         'username' => 'mongoadmin',
>         'password' => '123456',
>         'database' => 'camers',
>          'options' => [
>              'db' => 'admin'
>          ]

With MYSQL there are no problems, but it is necessary with монго a code

You need to set the "connection service". By default models look for the "db" service. You need to tell them to look for the "mongo" service: https://docs.phalcon.io/vi/3.3/db-odm#multiple-databases