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 thedb_reader
connection, and the rest can still be queried usingdb
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 :)
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
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.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Phalcon is a special PHP framework, the source code of which is written in C. It may seem strange, but thanks to this approach, this framework can be called the fastest project of its kind. Working with this framework does not mean you need to have C development experience. A programmer choosing Phalcon can only deal with PHP classes and namespaces generated by the framework.
https://phalcon.io/en-us
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.