From ae908199eb41fe7806a482c32ecbac1d23ae4b7b Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 9 May 2016 11:56:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E5=A2=9E=E5=8A=A0resultset=5Ftype=20?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E8=AE=BE=E7=BD=AE=E6=95=B0=E6=8D=AE=E9=9B=86?= =?UTF-8?q?=E7=9A=84=E8=BF=94=E5=9B=9E=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/db/Connection.php | 47 ++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/library/think/db/Connection.php b/library/think/db/Connection.php index 810f1246..2d7ebaa3 100644 --- a/library/think/db/Connection.php +++ b/library/think/db/Connection.php @@ -65,37 +65,39 @@ abstract class Connection // 数据库连接参数配置 protected $config = [ // 数据库类型 - 'type' => '', + 'type' => '', // 服务器地址 - 'hostname' => '', + 'hostname' => '', // 数据库名 - 'database' => '', + 'database' => '', // 用户名 - 'username' => '', + 'username' => '', // 密码 - 'password' => '', + 'password' => '', // 端口 - 'hostport' => '', + 'hostport' => '', // 连接dsn - 'dsn' => '', + 'dsn' => '', // 数据库连接参数 - 'params' => [], + 'params' => [], // 数据库编码默认采用utf8 - 'charset' => 'utf8', + 'charset' => 'utf8', // 数据库表前缀 - 'prefix' => '', + 'prefix' => '', // 数据库调试模式 - 'debug' => false, + 'debug' => false, // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器) - 'deploy' => 0, + 'deploy' => 0, // 数据库读写是否分离 主从式有效 - 'rw_separate' => false, + 'rw_separate' => false, // 读写分离后 主服务器数量 - 'master_num' => 1, + 'master_num' => 1, // 指定从服务器序号 - 'slave_no' => '', + 'slave_no' => '', // 是否严格检查字段是否存在 - 'fields_strict' => true, + 'fields_strict' => true, + // 数据集返回类型 + 'resultset_type' => Db::RESULTSET_ARRAY, ]; // PDO连接参数 @@ -233,7 +235,10 @@ abstract class Connection } // 记录当前字段属性大小写设置 $this->attrCase = $params[PDO::ATTR_CASE]; - + // 记录数据集返回类型 + if (isset($config['resultset_type'])) { + $this->resultSetType = $config['resultset_type']; + } try { if (empty($config['dsn'])) { $config['dsn'] = $this->parseDsn($config); @@ -453,16 +458,16 @@ abstract class Connection { $result = $this->PDOStatement->fetchAll($this->fetchType); $this->numRows = count($result); + // 返回指定对象类 + if (!empty($class)) { + return new $class($result); + } switch ($this->resultSetType) { case Db::RESULTSET_COLLECTION: // 返回数据集Collection对象 $result = new Collection($result); break; case Db::RESULTSET_CLASS: - // 返回指定对象类 - if (!empty($class)) { - $result = new $class($result); - } break; case Db::RESULTSET_ARRAY: default: