原文地址 http://wtbis.cn/q/9505
根據(jù)這個(gè)里面的實(shí)現(xiàn)還是要先在thinkorm.php 里面先定義好數(shù)據(jù)庫(kù)連接信息.這個(gè)文件不配置數(shù)據(jù)庫(kù)的信息
我這邊是數(shù)據(jù)庫(kù)配置信息都是存在數(shù)據(jù)庫(kù)
然后在控制器調(diào)用的時(shí)候傳入數(shù)據(jù)庫(kù)連接信息數(shù)組傳入。
根據(jù)這個(gè)傳入的數(shù)據(jù)庫(kù)配置信息來(lái)執(zhí)行查詢操作
模型
<?php
namespace app\api\model;
use think\Model;
use think\facade\Db;
/**
* 用戶
*/
class UserModel extends Model
{
// 表名
protected $table = 'base_user';
protected $connection;
public function __construct($connection)
{
$this->connection = $connection;
parent::construct($connection);
}
}
控制器調(diào)用
$dd = [
// 數(shù)據(jù)庫(kù)類型
'type' => 'mysql',
// 服務(wù)器地址
'hostname' => '127.0.0.1',
// 數(shù)據(jù)庫(kù)名
'database' => 'ceshi',
// 數(shù)據(jù)庫(kù)用戶名
'username' => 'root',
// 數(shù)據(jù)庫(kù)密碼
'password' => 'root',
// 數(shù)據(jù)庫(kù)連接端口
'hostport' => '3306',
// 數(shù)據(jù)庫(kù)連接參數(shù)
'params' => [
// 連接超時(shí)3秒
\PDO::ATTR_TIMEOUT => 3,
],
// 數(shù)據(jù)庫(kù)編碼默認(rèn)采用utf8
'charset' => 'utf8mb4',
// 數(shù)據(jù)庫(kù)表前綴
'prefix' => '',
// 斷線重連
'break_reconnect' => true,
// 關(guān)閉SQL監(jiān)聽(tīng)日志
'trigger_sql' => true,
// 自定義分頁(yè)類
'bootstrap' => '',
];
$model = new UserModel($dd);
$res = $model->select();
這樣測(cè)試不可以
最新