ThinkPHP 笔记

一、数据库连接信息可以在convention.php中作总的配置。

 'DB_TYPE'               =>  'mysql',//'mongo',     // 数据库类型
 'DB_HOST'               =>  'localhost', // 服务器地址
 'DB_NAME'               =>  'think_test',//'testDB',          // 数据库名
 'DB_USER'               =>  'root',      // 用户名
 'DB_PWD'                =>  'root',          // 密码
 'DB_PORT'               =>  '3306',//'27017',        // 端口
 'DB_PREFIX'             =>  'te_',    // 数据库表前缀

 

也可以在每个模型里面单独设置不同的连接配置

namespace Admin\Model;
use Think\Model;
class UserModel extends Model{
    protected $connection = array( //配置信息
        'db_type'  => 'mysql',
        'db_user'  => 'root',
        'db_pwd'   => 'root',
        'db_host'  => 'localhost',
        'db_port'  => '3306',
        'db_name'  => 'think_test2',
        'db_charset' =>'utf8',
    );
}

 

这是因为在Model.class.php中有此行:

 public function __construct(){
   ...
$this->db(0,empty($this->connection)?$connection:$this->connection,true);
   ... }

先读取原模型的配置信息,没有的话再读取总配置。

 

二、在模型的方法中,动态的使用其他数据库。

$info=$this->db(1,'mysql://root:root@localhost:3306/think_test#utf8')->table('te_info')->limit(2)->select();

 

posted @ 2015-11-19 11:34  fover的天地  阅读(146)  评论(0)    收藏  举报