diff --git a/library/think/Validate.php b/library/think/Validate.php index 9dada9de..afbb0799 100644 --- a/library/think/Validate.php +++ b/library/think/Validate.php @@ -602,7 +602,7 @@ class Validate if (is_string($rule)) { $rule = explode(',', $rule); } - $db = Db::table($rule[0]); + $db = Db::name($rule[0]); $field = isset($rule[1]) ? $rule[1] : $field; if (strpos($field, '^')) { @@ -617,7 +617,7 @@ class Validate $map[$field] = $data[$field]; } - $key = strval(isset($rule[3]) ? $rule[3] : 'id'); + $key = strval(isset($rule[3]) ? $rule[3] : $db->getPk()); if (isset($rule[2])) { $map[$key] = ['neq', $rule[2]]; } elseif (isset($data[$key])) { diff --git a/library/think/db/Query.php b/library/think/db/Query.php index 4c920b51..902178c6 100644 --- a/library/think/db/Query.php +++ b/library/think/db/Query.php @@ -911,6 +911,17 @@ class Query return $fetch ? $_info[$guid][$fetch] : $_info[$guid]; } + /** + * 获取当前模型对象的主键 + * @access public + * @param string $table 数据表名 + * @return mixed + */ + public function getPk($table = '') + { + return $this->getTableInfo($table, 'pk'); + } + /** * 参数绑定 * @access public @@ -1044,7 +1055,7 @@ class Query */ protected function parsePkWhere($data, &$options) { - $pk = $this->getTableInfo($options['table'], 'pk'); + $pk = $this->getPk($options['table']); // 获取当前数据表 if (!empty($options['alias'])) { $alias = $options['alias']; @@ -1137,7 +1148,7 @@ class Query { $options = $this->parseExpress(); if (empty($options['where'])) { - $pk = $this->getTableInfo($options['table'], 'pk'); + $pk = $this->getPk($options['table']); // 如果存在主键数据 则自动作为更新条件 if (is_string($pk) && isset($data[$pk])) { $where[$pk] = $data[$pk]; @@ -1333,7 +1344,7 @@ class Query */ public function chunk($count, $callback, $column = null) { - $column = $column ?: $this->getTableInfo('', 'pk'); + $column = $column ?: $this->getPk(); $options = $this->getOptions(); $resultSet = $this->limit($count)->order($column, 'asc')->select();