Cakephp delete不适用于多数据库连接
我在使用CakePHP2.4,我在一个项目中工作,该项目基于登录的用户使用两个db连接。当访问第二个DB并检索数据时,它工作正常,但当我删除第二个DB表中的记录时,将其抛出错误记录为Cakephp delete不适用于多数据库连接,cakephp,Cakephp,我在使用CakePHP2.4,我在一个项目中工作,该项目基于登录的用户使用两个db连接。当访问第二个DB并检索数据时,它工作正常,但当我删除第二个DB表中的记录时,将其抛出错误记录为 Invalid catalog name: 1046 No database selected 有人能帮忙吗 这是我的数据库连接代码 $this->default['host'] = 'localhost'; $this->default['login'] = 'xxxxxx'; $this->
Invalid catalog name: 1046 No database selected
有人能帮忙吗
这是我的数据库连接代码
$this->default['host'] = 'localhost';
$this->default['login'] = 'xxxxxx';
$this->default['password'] = 'xxxx';
$this->default['database'] = 'xxxxx';
$this->default['prefix'] = 'xxxx_';
if(!empty($_SESSION['subsites'])){
$this->test['host'] = 'localhost';
$this->test['login'] = 'xxxx';
$this->test['password'] = 'xxxxx';
$this->test['database'] = 'ccxxxx';
$this->test['prefix'] = 'xxxx'.'_';
}
然后我们使用$useDBconfig='test';在模型中,在app/config/database.php中将多个数据库配置定义为
public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'db1',
'prefix' => '',
//'encoding' => 'utf8',
);
public $subSites = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'db2',
'prefix' => '',
//'encoding' => 'utf8',
);
现在,当您想切换到子网站配置时,只需编写以下代码:
$this->Model->useDbConfig = $subSites;
您可以在模型中使用$useDbConfig属性
class Example extends AppModel {
public $useDbConfig = 'user';
}
在控制器内,只需使用:
$this->ModelName->useDbConfig = 'user';
有关更多详细信息,请单击是否可以粘贴处理数据库切换的代码?