Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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);
        }
        $app->mount($collection);
    }

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

$app = new \Phalcon\Mvc\Micro();
$app->setDI($di);
$app->handle($route);

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 :)



85.4k

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', '10.0.5.1', '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}


2.8k
Accepted
answer

:) 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 :)