You can select in different moments the connection you want to use for executing the query using setConnectionService(), setReadConnectionService(), setWriteConnectionService().
If I understand correctly what you wanted to do, you can try like this:
public/index.php
//Set the database service
$di->set('db_read', function() use ($config){
return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
"host" => $config->database_read->host,
"username" => $config->database_read->username,
"password" => $config->database_read->password,
"dbname" => $config->database_read->dbname,
"charset" => $config->database_read->encoding
));
});
$di->set('db_write', function() use ($config){
return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
"host" => $config->database_write->host,
"username" => $config->database_write->username,
"password" => $config->database_write->password,
"dbname" => $config->database_write->dbname,
"charset" => $config->database_write->encoding
));
});
And execute the query:
$content = new Content();
$content->setReadConnectionService('db_read');
$result = $content->query()
->where('type = :type:')
->bind(array('type' => 1))
->order('id DESC')
->limit(20)
->execute();
If you are doing raw queries, there are also other ways to select the connection you want:
public function getContents(){
$sql = "SELECt * FROM content LIMIT 10";
$content = new Content();
$result = $content->getDI()->get('db_read')->query($sql);
return new Resultset(null, $content, $result);
}