原文地址 http://www.wtbis.cn/q/9505
根據(jù)這個(gè)里面的實(shí)現(xiàn)還是要先在thinkorm.php 里面先定義好數(shù)據(jù)庫連接信息.這個(gè)文件不配置數(shù)據(jù)庫的信息
我這邊是數(shù)據(jù)庫配置信息都是存在數(shù)據(jù)庫
然后在控制器調(diào)用的時(shí)候傳入數(shù)據(jù)庫連接信息數(shù)組傳入。
根據(jù)這個(gè)傳入的數(shù)據(jù)庫配置信息來執(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ù)庫類型
'type' => 'mysql',
// 服務(wù)器地址
'hostname' => '127.0.0.1',
// 數(shù)據(jù)庫名
'database' => 'ceshi',
// 數(shù)據(jù)庫用戶名
'username' => 'root',
// 數(shù)據(jù)庫密碼
'password' => 'root',
// 數(shù)據(jù)庫連接端口
'hostport' => '3306',
// 數(shù)據(jù)庫連接參數(shù)
'params' => [
// 連接超時(shí)3秒
\PDO::ATTR_TIMEOUT => 3,
],
// 數(shù)據(jù)庫編碼默認(rèn)采用utf8
'charset' => 'utf8mb4',
// 數(shù)據(jù)庫表前綴
'prefix' => '',
// 斷線重連
'break_reconnect' => true,
// 關(guān)閉SQL監(jiān)聽日志
'trigger_sql' => true,
// 自定義分頁類
'bootstrap' => '',
];
$model = new UserModel($dd);
$res = $model->select();
這樣測試不可以
最新