我有几个客户端具有相同的数据库体系结构(mysql)和相同的解决方案(没有特定的开发)问题,现在我为每个客户端和每个config.php文件执行目录,我连接到数据库,如下所示:
对于客户x:
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => '***',
'password' => '***',
'database' => 'comuniksales_x',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
和客户y:
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => '***',
'password' => '***',
'database' => 'comuniksales_y',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
如您所见,我只更改了数据库的名称
N.B:每个数据库中都有客户端的标识符
我在身份验证页面中添加了一个新文本字段,供客户端输入帐户(例如:客户端x - > x,客户端y - > y,...),以便我可以检索x和To放入配置但我没有到达 field in the authentification
答案 0 :(得分:1)
您必须为第二个数据库设置不同的索引,例如$db['customer_y']
,因此您的$db['default']
示例中将包含2个数据库,$db['customer_y']
和config/database.php
:
$db['client_x'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => '***',
'password' => '***',
'database' => 'comuniksales_x',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
$db['client_y'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => '***',
'password' => '***',
'database' => 'comuniksales_y',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
然后在你的Controller中(如果client = x或y你获取输入的地方),如果客户端是x,你什么都不做,你使用默认值。但如果客户端是y,则加载$db['customer_y']
,如下所示:
$customer = $this->input->post('customer'); //or how you grab the customer
if($customer == 'x') {
$this->db = $this->load->database('customer_x', TRUE);
//here you can store the x in session and use it for the rest Controllers. If you want to use session, you'll have to add the condition in if()
}elseif($customer == 'y') {
$this->db = $this->load->database('customer_y', TRUE);
//here you can store the y in session and use it for the rest Controllers. If you want to use session, you'll have to add the condition in if()
}
如果您想要更具体,可以将config/database.php
和使用索引customer_x
(而不是默认值)和customer_y
分开。不要忘记更改$active_group
config/database.php
答案 1 :(得分:1)
可能的方法是设置MY_Model
这样的事情应该做的工作
class MY_Model extends CI_Model
{
protected $customDb;
public function __construct()
{
parent::__construct();
$this->loadCustomerDatabase();
}
private function loadCustomerDatabase()
{
//define your customers database
$customerDatabase = $this->session->userdata("customer_db");
$config['hostname'] = 'localhost';
$config['username'] = 'myusername';
$config['password'] = 'mypassword';
$config['database'] = $customerDatabase;
$config['dbdriver'] = 'mysqli';
$config['dbprefix'] = '';
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = '';
$config['char_set'] = 'utf8';
$config['dbcollat'] = 'utf8_general_ci';
$this->customDb = $this->load->database($config, true);
}
}
并且所有模型都应扩展MY_Model,您可以直接访问customDB。
答案 2 :(得分:0)
要将所有客户端设置放在同一个文件中并选择匹配的配置,请尝试这种方法。
这是database.php配置文件
$db['clientx'] = array(
... all the fields ...
);
$db['clienty'] = array(
... all the fields ...
);
$db['clientz'] = array(
... all the fields ...
);
然后读取客户端名称的登录表单,并使用它来选择连接配置并像这样建立数据库连接。
$client = $this->input->post('clientID');
$this->load->database($client);
您无需使用第二个参数来加载数据库,例如
$this->load->database($client, TRUE);
因为如果不使用第二个参数,则会自动将连接分配给CI对象。换句话说,它不适合你。
我看到的唯一问题是用户是否发布了与数据库配置文件中的客户端名称不匹配的客户端名称。如果找不到配置,Codeigniter将不会加载数据库,并将显示错误页面。但这并没有给你一种优雅的回应方式,让用户知道该怎么做。在尝试加载db config之前,我没有快速解决方案来“验证”用户输入。
在调用$this->load->database
之前,您可能需要加载配置文件并自行检查“密钥”。查看核心文件/system/database/DB.php
,了解如何处理$params
到function &DB(...)
的参数。