0

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 :)

2 Answers 2

0

Well I think I would just use the query builder like

$sql = 'SELECT * FROM robots WHERE id > 0';

// Base model
$robot = new Robots();

// Execute the query
return new Resultset(
     null,
     $robot,
     $robot->getReadConnection()->query($sql)
);

Reference: https://docs.phalcon.io/3.4/en/db-phql#creating-queries-using-the-query-builder

2
  • That's not exactly what I was trying to do. If I already set the read connection trough the Model then it's unnecessary to specify the read connection when making a simple non locking "SELECT". What I'm actually aiming for is to use some form of connection which is regularly detached from the models, but still available for use on demand as you showed here.
    – Deanz
    Sep 24, 2022 at 6:29
  • Well maybe I don't get the problem. But if you want to use a different read connection than defined in the Model itself you can still instantiate a new object from the model and call $robot->setReadConnectionService & provide your desired db connection you set in your di. Afterwards you can use the query builder as described above. In this scenario you would only set this specific db connection on the instantiated model object. setReadConnectionService is a public method inherited by extending the Phalcon\Mvc\Model class. I hope I got your challenge right, otherwise sorry for misunderstanding Sep 27, 2022 at 12:16
0

With the assumption I understand what you are asking is specifying connection based on query. This also assumes you have separate functions for each query. So you could do something such as get_class_methods() to get the actions available and compare that to the current one it is executing. Based on that you can set the connection.

...
use Phalcon\Mvc\Dispatcher;

{
public $functions;

public $read_functions;

public function initialize(Dispatcher $dispatcher)
{
    $action = $dispatcher->getActionName();
     //define this however you want -- several ways to define this
    $this->read_functions = array('some_function');
    $this->functions = get_class_methods('someController');
    
    if(in_array($action, $this->read_functions)){
        $this->setConnectionService('db_reader');
    }else{
        $this->setConnectionService('db');
    }
}

public function some_function()
{
    //I only read things using db_reader
}

public function some_function()
{
    //I use db
}

You can expand this to use a switch and use case statements to do the logic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.