From ae9a5a7bcaeab42c1435c46f880e25be81b61e3c Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 19 Jan 2016 18:54:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3Model=E7=B1=BB=E7=9A=84=5Fwri?= =?UTF-8?q?te=5Fdata=E6=96=B9=E6=B3=95=E6=95=B0=E6=8D=AE=E6=A3=80=E6=B5=8B?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E4=B8=8D=E5=88=B0fields=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 22 +++++++++------ library/think/db/Driver.php | 56 ++++++++++++++++++++++++------------- 2 files changed, 49 insertions(+), 29 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index 7982b3c2..9e0a9302 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -190,14 +190,15 @@ class Model } } } + $fields = $this->getDbFields(); // 检查非数据字段 - if (!empty($this->fields)) { + if (!empty($fields)) { foreach ($data as $key => $val) { - if (!in_array($key, $this->fields, true)) { + if (!in_array($key, $fields, true)) { unset($data[$key]); } elseif (is_scalar($val) && empty($this->options['bind'][':' . $key])) { // 字段类型检查 - $this->_parseType($data, $key); + $this->_parseType($data, $key, $this->options['bind']); } } } @@ -777,7 +778,7 @@ class Model $key = trim($key); if (in_array($key, $fields, true)) { if (is_scalar($val) && empty($options['bind'][':' . $key])) { - $this->_parseType($options['where'], $key); + $this->_parseType($options['where'], $key, $options['bind']); } } } @@ -799,18 +800,21 @@ class Model * @param string $key 字段名 * @return void */ - protected function _parseType(&$data, $key) + protected function _parseType(&$data, $key, &$bind) { - if (!isset($this->options['bind'][':' . $key]) && isset($this->fields['_type'][$key])) { + if (!isset($bind[':' . $key]) && isset($this->fields['_type'][$key])) { $fieldType = strtolower($this->fields['_type'][$key]); if (false !== strpos($fieldType, 'enum')) { // 支持ENUM类型优先检测 } elseif (false === strpos($fieldType, 'bigint') && false !== strpos($fieldType, 'int')) { - $data[$key] = intval($data[$key]); + $bind[':' . $key] = [$data[$key], \PDO::PARAM_INT]; + $data[$key] = ':' . $key; } elseif (false !== strpos($fieldType, 'float') || false !== strpos($fieldType, 'double')) { - $data[$key] = floatval($data[$key]); + $bind[':' . $key] = [$data[$key], \PDO::PARAM_INT]; + $data[$key] = ':' . $key; } elseif (false !== strpos($fieldType, 'bool')) { - $data[$key] = (bool) $data[$key]; + $bind[':' . $key] = [$data[$key], \PDO::PARAM_BOOL]; + $data[$key] = ':' . $key; } elseif (false !== strpos($fieldType, 'json') && is_array($data[$key])) { $data[$key] = json_encode($data[$key]); } diff --git a/library/think/db/Driver.php b/library/think/db/Driver.php index 0e0e8937..64573625 100644 --- a/library/think/db/Driver.php +++ b/library/think/db/Driver.php @@ -41,23 +41,39 @@ abstract class Driver protected $_linkID = null; // 数据库连接参数配置 protected $config = [ - 'type' => '', // 数据库类型 - 'hostname' => '127.0.0.1', // 服务器地址 - 'database' => '', // 数据库名 - 'username' => '', // 用户名 - 'password' => '', // 密码 - 'hostport' => '', // 端口 - 'dsn' => '', // - 'params' => [], // 数据库连接参数 - 'charset' => 'utf8', // 数据库编码默认采用utf8 - 'prefix' => '', // 数据库表前缀 - 'debug' => false, // 数据库调试模式 - 'deploy' => 0, // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器) - 'rw_separate' => false, // 数据库读写是否分离 主从式有效 - 'master_num' => 1, // 读写分离后 主服务器数量 - 'slave_no' => '', // 指定从服务器序号 - 'db_like_fields' => '', // like字段自动替换为%%包裹 - 'debug' => false, // 是否调试 + // 数据库类型 + 'type' => '', + // 服务器地址 + 'hostname' => '127.0.0.1', + // 数据库名 + 'database' => '', + // 用户名 + 'username' => '', + // 密码 + 'password' => '', + // 端口 + 'hostport' => '', + 'dsn' => '', + // 数据库连接参数 + 'params' => [], + // 数据库编码默认采用utf8 + 'charset' => 'utf8', + // 数据库表前缀 + 'prefix' => '', + // 数据库调试模式 + 'debug' => false, + // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器) + 'deploy' => 0, + // 数据库读写是否分离 主从式有效 + 'rw_separate' => false, + // 读写分离后 主服务器数量 + 'master_num' => 1, + // 指定从服务器序号 + 'slave_no' => '', + // like字段自动替换为%%包裹 + 'db_like_fields' => '', + // 是否开启数据库调试 + 'debug' => false, ]; // 数据库表达式 protected $exp = ['eq' => '=', 'neq' => '<>', 'gt' => '>', 'egt' => '>=', 'lt' => '<', 'elt' => '<=', 'notlike' => 'NOT LIKE', 'like' => 'LIKE', 'in' => 'IN', 'notin' => 'NOT IN', 'not in' => 'NOT IN', 'between' => 'BETWEEN', 'not between' => 'NOT BETWEEN', 'notbetween' => 'NOT BETWEEN']; @@ -409,7 +425,7 @@ abstract class Driver } elseif (is_scalar($val)) { // 过滤非标量数据 if (0 === strpos($val, ':') && in_array($val, array_keys($this->bind))) { - $set[] = $this->parseKey($key) . '=' . $this->quote($val); + $set[] = $this->parseKey($key) . '=' . $val; } else { $name = count($this->bind); $set[] = $this->parseKey($key) . '=:' . $name; @@ -890,7 +906,7 @@ abstract class Driver // 过滤非标量数据 $fields[] = $this->parseKey($key); if (0 === strpos($val, ':') && in_array($val, array_keys($this->bind))) { - $values[] = $this->parseValue($val); + $values[] = $val; } else { $name = count($this->bind); $values[] = ':' . $name; @@ -932,7 +948,7 @@ abstract class Driver $value[] = 'NULL'; } elseif (is_scalar($val)) { if (0 === strpos($val, ':') && in_array($val, array_keys($this->bind))) { - $value[] = $this->parseValue($val); + $value[] = $val; } else { $name = count($this->bind); $value[] = ':' . $name;