Php 如何在phalcon框架中同时连接多个数据库在模型类中使用两者而不是仅使用一个
在我的代码中,我有两个数据库Php 如何在phalcon框架中同时连接多个数据库在模型类中使用两者而不是仅使用一个,php,database,phalcon,Php,Database,Phalcon,在我的代码中,我有两个数据库ABC和XYZ。我想在同一个模型中使用这两个数据库,而不是在phalcon中它的解决方案是什么?如何为此实现多个数据库连接?不能在同一型号中同时使用两个数据库连接。因此: // Set the connection in the DI $di->set('database_slave', .....) $di->set('database_master', .....) 在您的模型中,您只能执行以下操作: public function initiali
ABC
和XYZ
。我想在同一个模型中使用这两个数据库,而不是在phalcon中它的解决方案是什么?如何为此实现多个数据库连接?不能在同一型号中同时使用两个数据库连接。因此:
// Set the connection in the DI
$di->set('database_slave', .....)
$di->set('database_master', .....)
在您的模型中,您只能执行以下操作:
public function initialize()
{
$this->setConnectionService('database_slave');
}
或
不能同时使用两者。要使模型更加灵活,您可以做的是如下扩展基本模型:
class MyModel extends \Phalcon\Mvc\Model
{
$connection = '';
public function initialize()
{
// Default to the master connection
$connection = ($this->connection) ? $this->connection : 'database_master';
$this->setConnectionService($connection);
parent::initialize()
}
public function setMyConnection($connection = 'database_master')
{
switch ($connection) {
case 'database_master':
case 'database_slave'
$this->connection = $connection;
break;
default:
$this->connection = 'database_master';
break;
}
}
}
在你的代码中你可以做到这一点
$mymodel = new MyModel();
$mymodel->setMyConnection('database_slave');
// do other stuff - this model will use the slave now.
如果确实希望从一个模型连接到两个数据库,则可以使用并实例化连接到模型内不同数据库的新对象。这是不可取的,但如果这是你想做的,那就去做吧
再看看这个:
一个
<?php
//This service returns a MySQL database
$di->set('dbMysql', function() {
return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
"host" => "localhost",
"username" => "root",
"password" => "secret",
"dbname" => "invo"
));
});
//This service returns a PostgreSQL database
$di->set('dbPostgres', function() {
return new \Phalcon\Db\Adapter\Pdo\PostgreSQL(array(
"host" => "localhost",
"username" => "postgres",
"password" => "",
"dbname" => "invo"
));
});
只需创建几个连接,并按所述使用它们。可能重复:-@Parry在您的链接中它是主从概念。我想问的是并行连接。如何将其扩展到几个以友好方式添加的碎片?您是否应该在服务器上保留一个包含所有碎片的配置文件(JSON、yaml或XML)?
<?php
//This service returns a MySQL database
$di->set('dbMysql', function() {
return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
"host" => "localhost",
"username" => "root",
"password" => "secret",
"dbname" => "invo"
));
});
//This service returns a PostgreSQL database
$di->set('dbPostgres', function() {
return new \Phalcon\Db\Adapter\Pdo\PostgreSQL(array(
"host" => "localhost",
"username" => "postgres",
"password" => "",
"dbname" => "invo"
));
});
<?php
class Robots extends \Phalcon\Mvc\Model
{
public function initialize()
{
$this->setConnectionService('dbPostgres');
}
}
<?php
class Robots extends \Phalcon\Mvc\Model
{
public function initialize()
{
$this->setReadConnectionService('dbSlave');
$this->setWriteConnectionService('dbMaster');
}
}
class Robots extends Phalcon\Mvc\Model
{
/**
* Dynamically selects a shard
*
* @param array $intermediate
* @param array $bindParams
* @param array $bindTypes
*/
public function selectReadConnection($intermediate, $bindParams, $bindTypes)
{
//Check if there is a 'where' clause in the select
if (isset($intermediate['where'])) {
$conditions = $intermediate['where'];
//Choose the possible shard according to the conditions
if ($conditions['left']['name'] == 'id') {
$id = $conditions['right']['value'];
if ($id > 0 && $id < 10000) {
return $this->getDI()->get('dbShard1');
}
if ($id > 10000) {
return $this->getDI()->get('dbShard2');
}
}
}
//Use a default shard
return $this->getDI()->get('dbShard0');
}
}
<?php
$robot = Robots::findFirst('id = 101');