From e4f45aa0cdd269269d29f30064a6472d7533c561 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 25 Apr 2016 15:04:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BValidate=E7=B1=BB=E7=9A=84uni?= =?UTF-8?q?uqe=E9=AA=8C=E8=AF=81=E8=A7=84=E5=88=99=20=E7=AC=AC=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E5=8F=82=E6=95=B0=E4=BC=A0=E5=85=A5=E4=B8=8D=E5=B8=A6?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E8=A1=A8=E5=89=8D=E7=BC=80=E7=9A=84=E8=A1=A8?= =?UTF-8?q?=E5=90=8D=20=E5=B9=B6=E4=B8=94=E6=94=AF=E6=8C=81=E4=B8=BB?= =?UTF-8?q?=E9=94=AE=E8=87=AA=E5=8A=A8=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Validate.php | 4 ++-- library/think/db/Query.php | 17 ++++++++++++++--- 2 files changed, 16 insertions(+), 5 deletions(-) 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();