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.

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.



8.5k
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?!!!



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

or

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