修正Model类的_write_data方法数据检测获取不到fields的问题

This commit is contained in:
thinkphp
2016-01-19 18:54:16 +08:00
parent 5dc8d8286c
commit ae9a5a7bca
2 changed files with 49 additions and 29 deletions

View File

@@ -190,14 +190,15 @@ class Model
} }
} }
} }
$fields = $this->getDbFields();
// 检查非数据字段 // 检查非数据字段
if (!empty($this->fields)) { if (!empty($fields)) {
foreach ($data as $key => $val) { foreach ($data as $key => $val) {
if (!in_array($key, $this->fields, true)) { if (!in_array($key, $fields, true)) {
unset($data[$key]); unset($data[$key]);
} elseif (is_scalar($val) && empty($this->options['bind'][':' . $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); $key = trim($key);
if (in_array($key, $fields, true)) { if (in_array($key, $fields, true)) {
if (is_scalar($val) && empty($options['bind'][':' . $key])) { 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 字段名 * @param string $key 字段名
* @return void * @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]); $fieldType = strtolower($this->fields['_type'][$key]);
if (false !== strpos($fieldType, 'enum')) { if (false !== strpos($fieldType, 'enum')) {
// 支持ENUM类型优先检测 // 支持ENUM类型优先检测
} elseif (false === strpos($fieldType, 'bigint') && false !== strpos($fieldType, 'int')) { } 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')) { } 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')) { } 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])) { } elseif (false !== strpos($fieldType, 'json') && is_array($data[$key])) {
$data[$key] = json_encode($data[$key]); $data[$key] = json_encode($data[$key]);
} }

View File

@@ -41,23 +41,39 @@ abstract class Driver
protected $_linkID = null; protected $_linkID = null;
// 数据库连接参数配置 // 数据库连接参数配置
protected $config = [ protected $config = [
'type' => '', // 数据库类型 // 数据库类型
'hostname' => '127.0.0.1', // 服务器地址 'type' => '',
'database' => '', // 数据库名 // 服务器地址
'username' => '', // 用户名 'hostname' => '127.0.0.1',
'password' => '', // 密码 // 数据库名
'hostport' => '', // 端口 'database' => '',
'dsn' => '', // // 用户名
'params' => [], // 数据库连接参数 'username' => '',
'charset' => 'utf8', // 数据库编码默认采用utf8 // 密码
'prefix' => '', // 数据库表前缀 'password' => '',
'debug' => false, // 数据库调试模式 // 端口
'deploy' => 0, // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器) 'hostport' => '',
'rw_separate' => false, // 数据库读写是否分离 主从式有效 'dsn' => '',
'master_num' => 1, // 读写分离后 主服务器数量 // 数据库连接参数
'slave_no' => '', // 指定从服务器序号 'params' => [],
'db_like_fields' => '', // like字段自动替换为%%包裹 // 数据库编码默认采用utf8
'debug' => false, // 是否调试 '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']; 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)) { } elseif (is_scalar($val)) {
// 过滤非标量数据 // 过滤非标量数据
if (0 === strpos($val, ':') && in_array($val, array_keys($this->bind))) { if (0 === strpos($val, ':') && in_array($val, array_keys($this->bind))) {
$set[] = $this->parseKey($key) . '=' . $this->quote($val); $set[] = $this->parseKey($key) . '=' . $val;
} else { } else {
$name = count($this->bind); $name = count($this->bind);
$set[] = $this->parseKey($key) . '=:' . $name; $set[] = $this->parseKey($key) . '=:' . $name;
@@ -890,7 +906,7 @@ abstract class Driver
// 过滤非标量数据 // 过滤非标量数据
$fields[] = $this->parseKey($key); $fields[] = $this->parseKey($key);
if (0 === strpos($val, ':') && in_array($val, array_keys($this->bind))) { if (0 === strpos($val, ':') && in_array($val, array_keys($this->bind))) {
$values[] = $this->parseValue($val); $values[] = $val;
} else { } else {
$name = count($this->bind); $name = count($this->bind);
$values[] = ':' . $name; $values[] = ':' . $name;
@@ -932,7 +948,7 @@ abstract class Driver
$value[] = 'NULL'; $value[] = 'NULL';
} elseif (is_scalar($val)) { } elseif (is_scalar($val)) {
if (0 === strpos($val, ':') && in_array($val, array_keys($this->bind))) { if (0 === strpos($val, ':') && in_array($val, array_keys($this->bind))) {
$value[] = $this->parseValue($val); $value[] = $val;
} else { } else {
$name = count($this->bind); $name = count($this->bind);
$value[] = ':' . $name; $value[] = ':' . $name;