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.

Connecting models in different databases with query builder

I'd like to connect two models that are in different databases using models and builder.

I'm using phalcon 1.3.4 and i have in example db A(table Invoices) and db B (table logs).

Now I'm trying to get data from db B someyhing like this :

    $logs = $this->di->get('modelsManager')
        ->createBuilder()
        ->columns(
            array(
                'log.code',
                'inv.codeA',
                'log.request',
                'log.response',
                'inv.createdAt'
            )
        )
        ->addFrom('App\Model\Logs', 'log')
        ->leftJoin('App\Model\Invoices', 'inv.code = log.code', 'inv')
        ->getQuery()
        ->execute();

And it keeps trying to join table invoices that is defined in db A. And keep geting

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'B.invoices' doesn't exist

I can join those tables in plain sql, probably rawSql approach would solve it but I'm wondering if it's possible doing models?



1.6k
Accepted
answer
edited Sep '15

Found solution !

In this case every model should have defined shema so ORM could use right database in query.

for model A

$this->setSchema('schemaA');

for model B

$this->setSchema('schemaB');

Cheers!