We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Micro Application with Controller and Database

Hi guys, I'm trying to setup a micro apllication with controllers. I initialized a DI and set 'db' :

    $di = new FactoryDefault();
    $di->set('db', function () use ($di) {
        $config = $di->getShared('config');
        $connection = new DbAdapter(array(
            'host' => $config->database->host,
            'username' => $config->database->username,
            'password' => $config->database->password,
            'dbname' => $config->database->dbname
        return $connection;

and set some collections for routing:

    foreach($this->config->api->controller AS $controller => $routes) {
        $collection = new Collection();
        $controller = 'Kaasahealth\\Api\\' . $controller;
        $collection->setHandler($controller, true);
        foreach($routes AS $method => $route) {
            $collection->get($route, $method);
            $collection->post($route, $method);

$this->config was loaded before :) than I try to start my app...

$app = new \Phalcon\Mvc\Micro();

On handle he calls the controller as expected but when I try to get data from my Model I get

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

.... How can I set the db Service?

Tanks for helping me :)


It seems Phalcon can't find the database service, could you please post the backtrace of the exception:

} catch (\Exception $e) {
    echo $e->getMessage();
    echo $e->getTraceAsString();
edited Aug '14
Service 'db' wasn't found in the dependency injection container
#0 [internal function]: Phalcon\DI->get('db', NULL)
#1 [internal function]: Phalcon\DI->getShared('db')
#2 [internal function]: Phalcon\Mvc\Model\Manager->getReadConnection(Object(CefDesign\Models\LogTypes))
#3 [internal function]: Phalcon\Mvc\Model->getReadConnection()
#4 [internal function]: Phalcon\Mvc\Model\MetaData\Strategy\Introspection->getMetaData(Object(CefDesign\Models\LogTypes), Object(Phalcon\DI\FactoryDefault))
#5 [internal function]: Phalcon\Mvc\Model\MetaData->_initialize(Object(CefDesign\Models\LogTypes), 'CefDesign\mod...', 'log_types', NULL)
#6 [internal function]: Phalcon\Mvc\Model\MetaData->readMetaDataIndex(Object(CefDesign\Models\LogTypes), 1)
#7 [internal function]: Phalcon\Mvc\Model\MetaData->getPrimaryKeyAttributes(Object(CefDesign\Models\LogTypes))
#8 [internal function]: Phalcon\Mvc\Model\Query\Builder->getPhql()
#9 [internal function]: Phalcon\Mvc\Model\Query\Builder->getQuery()
#10 /var/www/admin.cef-design.de/Application/Api/LogApi.php(20): Phalcon\Mvc\Model::findFirst(5)
#11 [internal function]: CefDesign\Api\LogApi->put('5', '', 'Mozilla%2F5.0+%...', 'question-1-btn-...', 'checked', 'uncheked')
#12 /var/www/admin.cef-design.de/Application/Utilities/GlobalDataProvider.php(132): Phalcon\Mvc\Micro->handle('/api/log/put/5/...')
#13 /var/www/shens.cef-design.de/app/controllers/ApiController.php(19): CefDesign\Utilities\GlobalDataProvider->process('/api/log/put/5/...')
#14 [internal function]: MeineApp\App\Controllers\ApiController->logAction('5', 'question-1-btn-...')
#15 [internal function]: Phalcon\Dispatcher->dispatch()
#16 /var/www/shens.cef-design.de/public/index.php(37): Phalcon\Mvc\Application->handle()
#17 {main}


:) I fixed my problem: I am using constants variables in both application, both constants have the same name.... After I changed this to different names it worked... thanks anyway :)

A model gets it connection from the model manager. The modelManager can only set different connections on seperate models. This is not the case in you problem, you want a different connection based on the state of the model. run 3 amusing running game

Thank you for providing the solution! Now I could apply it for the best no deposit bonus codes you can use for your game. But for today you will meet less bugs.