修正Model类

This commit is contained in:
thinkphp
2016-02-03 08:34:18 +08:00
parent f2ac964ea7
commit 3964468843

View File

@@ -234,7 +234,7 @@ class Model
unset($data[$key]); unset($data[$key]);
} elseif (is_scalar($val) && !isset($this->options['bind'][$key])) { } elseif (is_scalar($val) && !isset($this->options['bind'][$key])) {
// 字段类型检查 // 字段类型检查
$this->_parseType($this->getTableName(), $data, $key, $this->options['bind']); $this->_parseType($data, $key, $this->options['bind']);
} }
} }
} }
@@ -793,7 +793,7 @@ class Model
if (isset($options['table'])) { if (isset($options['table'])) {
// 动态指定表名 // 动态指定表名
$fields = $this->getTableInfo($options['table'], 'fields'); $fields = $this->getTableInfo('fields', $options['table']);
} else { } else {
$options['table'] = $this->getTableName(); $options['table'] = $this->getTableName();
$fields = $this->getFields(); $fields = $this->getFields();
@@ -808,7 +808,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['table'], $options['where'], $key, $options['bind']); $this->_parseType($options['where'], $key, $options['bind'], $options['table']);
} }
} }
} }
@@ -826,15 +826,15 @@ class Model
/** /**
* 数据类型检测 * 数据类型检测
* @access protected * @access protected
* @param string $table 表名
* @param array $data 数据 * @param array $data 数据
* @param string $key 字段名 * @param string $key 字段名
* @param array $bind 参数绑定列表 * @param array $bind 参数绑定列表
* @param string $tableName 表名
* @return void * @return void
*/ */
protected function _parseType($table, &$data, $key, &$bind) protected function _parseType(&$data, $key, &$bind, $tableName = '')
{ {
$binds = $this->getTableInfo($table, 'bind'); $binds = $this->getTableInfo('bind', $tableName);
$bind[$key] = [$data[$key], isset($binds[$key]) ? $binds[$key] : \PDO::PARAM_STR]; $bind[$key] = [$data[$key], isset($binds[$key]) ? $binds[$key] : \PDO::PARAM_STR];
$data[$key] = ':' . $key; $data[$key] = ':' . $key;
} }
@@ -1098,13 +1098,13 @@ class Model
/** /**
* 获取当前主键名称 * 获取当前主键名称
* @access public * @access public
* @param string $tableName 数据表名 留空自动获取
* @return mixed * @return mixed
*/ */
public function getPk() public function getPk($tableName = '')
{ {
if (is_null($this->pk)) { if (is_null($this->pk)) {
$tableName = isset($this->options['table']) ? $this->options['table'] : $this->getTableName(); $this->pk = $this->getTableInfo('pk', $tableName);
$this->pk = $this->getTableInfo($tableName, 'pk');
} }
return $this->pk; return $this->pk;
} }
@@ -1112,12 +1112,13 @@ class Model
/** /**
* 获取当前字段信息 * 获取当前字段信息
* @access public * @access public
* @param string $tableName 数据表名 留空自动获取
* @return array * @return array
*/ */
public function getFields() public function getFields($tableName = '')
{ {
if (empty($this->fields)) { if (empty($this->fields)) {
$this->fields = $this->getTableInfo('', 'fields'); $this->fields = $this->getTableInfo('fields', $tableName);
} }
return $this->fields; return $this->fields;
} }
@@ -1125,13 +1126,15 @@ class Model
/** /**
* 获取数据表信息 * 获取数据表信息
* @access public * @access public
* @param string $tableName 数据表名 留空自动获取
* @param string $fetch 获取信息类型 包括 fields type bind pk * @param string $fetch 获取信息类型 包括 fields type bind pk
* @param string $tableName 数据表名 留空自动获取
* @return mixed * @return mixed
*/ */
public function getTableInfo($tableName = '', $fetch = '') public function getTableInfo($fetch = '', $tableName = '')
{ {
$tableName = $tableName ?: $this->getTableName(); if (!$tableName) {
$tableName = isset($this->options['table']) ? $this->options['table'] : $this->getTableName();
}
$guid = md5($tableName); $guid = md5($tableName);
$result = Cache::get($guid); $result = Cache::get($guid);
if (!$result) { if (!$result) {