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

Database connection problem in Models using Micro

I'm creating a RESTful API in Phalcon PHP, I set the DI and the database information as always using:

This is how I'm configuring the database in the DI.

use Phalcon\Db\Adapter\Pdo\Postgresql as DbAdapter;
$di = new FactoryDefault();

$di['db'] = function() use ($config) {
return new DbAdapter(array(
            "host" => $config->database->host,
            "username" => $config->database->username,
            "password" => $config->database->password,
            "dbname" => $config->database->dbname,
            "schema" => $config->database->schema,
        ));
};

Then setting the DI

$app->setDi($di);

And I'm handling the actions like this:

//Gets a client information
$app->get('/client', array(new ClientController, 'getAction'));

The problem is that when ClientController creates a new model object it simply returns an error saying that the table was not found and I think is because it is not loading the database information from the DI, so my question is, how can I set up that information globally, is it not supposed that Phalcon do it?

The database works fine in the main application controller, but I want to order the methods in other different subcontrollers and there is where the database config doesn't work.

I'm doing this as stated in the documentation:

https://docs.phalcon.io/es/latest/reference/micro.html

Thanks.



43.9k

Please let us see how you have registred your db service in the DI

I updated the information in the first post, hope you understand.

Thanks.



43.9k
edited Oct '14

db config looks okay, also you said that db access is working fine with another controller (<=> your config file is correct) , so maybe in your ClientController -> getAction you call a specific model (saying ClientModel wich is different from the one you call in MainController). Perhaps setting the source in your ClientModel will help (because phalcon complains that it is not finding the table):

public function getSource()
    {
        return "the_robots";
    }


12.8k
Accepted
answer

I discovered that is getting the DI and the problem now is that the Database adapter is not using the schema I set up when creating the adapter, I opened another discussion here, also I'm having this problem frequently with PostgreSQL.

https://forum.phalcon.io/discussion/2251/database-adapter-not-setting-schemas-using-postgresql

Greetings!