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 Adapter not setting schemas in PostgreSQL

Hi.

I'm having a problem with my micro application, I use PostgreSQL as database, but the database adapter I create don't set the schema for the models and I need to do it manually using the models manager, how can I avoid setting the schema manually for every model?

This is how I'm creating the Adapter

use Phalcon\Db\Adapter\Pdo\Postgresql as DbAdapter;
...

return new DbAdapter(array(
    "host" => $config->database->host,
    "username" => $config->database->username,
    "password" => $config->database->password,
    "dbname" => $config->database->dbname,
    "schema" => $config->database->schema,
));

This doesn't work

$person = new Person;
$person = $person->findFirst(1);

This works:

$person = new Person;
$app->modelsManager->setModelSchema($person, 'myschema');
$person = $person->findFirst(1);

If I don't set the schema manually then the model object can't find the tables:

Table "person" doesn't exist on database when dumping meta-data for Person.

I think the script is always reading the public schema instead of the one I set up.

Thanks.



12.8k
Accepted
answer

I'm solving this issue setting the schema in the model initialize, which I don't think is necessary since Phalcon should do it automatically, I think is a bug that I'm going to report.

Here is the code that must be added in the model:

    public function initialize() {
        $this->getModelsManager()->setModelSchema($this, 'the_schema_name');
    }

Greetings!

Any other idea?!!!



2.5k
public function initialize()
{
    $this->setSchema('the_schema_name');
}

or

public function getSchema()
{
    return 'the_schema_name';
}