PHP Phalcon 3.4 - Can I specify connection per query , using Phalcons/Model query methods?

Viewed 39

sorry if it'll be a bit messy (English is not my native tongue so excuse me for anything not clear enough!)

I'm Using Phalcon 3.4, with PHP 7.3.16

Let's say I have got a basic setup of

class A extends Model {...}
class AController extends Controller {...}

I've set up 2 separate connections to the DB in the DI

// adapter using read / write connection
$di->set('db', function() {
    return new ...
});
  
// adapter using read only connection
$di->set('db_reader', function() {
    return new ...
});

db service acts as the default connections when querying using the Models (::find(), ::query(), ->save())

the question is, can I force a specific connection to a specific query, from the controller?

I know I can

class A extends Model {
    public function initialize() {
        $this->setReadConnectionService('db_reader');
        $this->setWriteConnectionService('db');
    }
}

but I want specific read operations happening in the controller, to use the db_reader connection, and the rest can still be queried using db which has the read/write permissions.

something like

class AController extends Controller {
    public function AAction() {
        $a = A::query()->setReadConnection('db_reader')->Where('....')....;
    }
}

is it possible?

Thanks ahead and sorry for the trouble of reading so far :)

1 Answers
Related