From 5f0abf0818fdb0f1a96001329cfcec2054ffba0a Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 16 Feb 2016 15:29:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E6=93=8D=E4=BD=9C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=BC=BA=E5=88=B6=E6=95=B0=E6=8D=AE=E8=BD=AC=E6=8D=A2?= =?UTF-8?q?=20=E4=BF=AE=E6=AD=A3=E5=8F=82=E6=95=B0=E7=BB=91=E5=AE=9A?= =?UTF-8?q?=E7=9A=84=E4=B8=80=E5=A4=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Model.php | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/library/think/Model.php b/library/think/Model.php index b395554d..362251f5 100644 --- a/library/think/Model.php +++ b/library/think/Model.php @@ -812,10 +812,8 @@ class Model // 对数组查询条件进行字段类型检查 foreach ($options['where'] as $key => $val) { $key = trim($key); - if (in_array($key, $fields, true)) { - if (is_scalar($val) && empty($options['bind'][$key])) { - $this->_parseType($options['where'], $key, $options['bind'], $options['table']); - } + if (in_array($key, $fields, true) && is_scalar($val) && empty($options['bind'][$key])) { + $this->_parseType($options['where'], $key, $options['bind'], $options['table']); } } } @@ -830,7 +828,7 @@ class Model {} /** - * 数据类型检测 + * 数据类型检测和自动转换 * @access protected * @param array $data 数据 * @param string $key 字段名 @@ -840,7 +838,20 @@ class Model */ protected function _parseType(&$data, $key, &$bind, $tableName = '') { - $binds = $this->getTableInfo('bind', $tableName); + $binds = $this->getTableInfo('bind', $tableName); + $type = $this->getTableInfo('type', $tableName); + // 强制类型转换 + if (false !== strpos($type[$key], 'int')) { + $data[$key] = (int) $data[$key]; + } elseif (false !== strpos($type[$key], 'float') || false !== strpos($type[$key], 'double')) { + $data[$key] = (float) $data[$key]; + } elseif (false !== strpos($type[$key], 'bool')) { + $data[$key] = (bool) $data[$key]; + } + if (':' == substr($data[$key], 0, 1) && isset($bind[substr($data[$key], 1)])) { + // 已经绑定 无需再次绑定 请确保bind方法优先执行 + return; + } $bind[$key] = [$data[$key], isset($binds[$key]) ? $binds[$key] : \PDO::PARAM_STR]; $data[$key] = ':' . $key; }