配置:
'DB_CONF_READ'=Array(
'DB_TYPE' =>'mysql',
'DB_HOST' =>'192.168.1.1',
'DB_NAME' =>'demo',
'DB_USER' =>'user',
'DB_PWD' =>'123',
'DB_PREFIX' => 'T_'),
'DB_CONF_WRITE' => 'mysql://user:pwd@localhost:3306/demo'
使用:
$mod = new Model('test_tab',NULL,'DB_CONF_WRITE');
$mod->db(1, 'mysql://user:pwd@localhost:3306/demo');//说明:这是后来加入的
$iId = $mod->add($data);在没有加入“说明”那段时,死活不能写入(insert)数据成功(select/update是成功的,不知道为什么)。后来,查看ThinkPHP源码的Model.class.php文件发现在构造函数中有这么一句:
$this->db(0,empty($this->connection)?$connection:$this->connection);试着修改了这句代码“0”,居然可以了。//增加$num参数,默认值0
public function __construct($name='',$tablePrefix='',$connection='',$num=0) {
...
$this->db($num,empty($this->connection)?$connection:$this->connection);
}这样修改后,上面的代码就可以使用:$mod = new Model('test_tab',NULL,'DB_CONF_WRITE');
$iId = $mod->add($data);不知道大家是如何使用多数据库操作的?还是本身ThinkPHP3.0中就存在这个问题呢?大家分享分享大家的见解啊!
最佳答案