From 4d01abcb299c84272e67ed364473bf7745f22c62 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Mon, 21 Mar 2016 16:55:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9Bunique=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E8=A7=84=E5=88=99=E7=9A=84=E5=A4=8D=E5=90=88=E4=B8=BB=E9=94=AE?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E7=9A=84=E9=94=99=E8=AF=AF=20=E5=A4=8D?= =?UTF-8?q?=E5=90=88=E4=B8=BB=E9=94=AE=E7=9A=84=E6=83=85=E5=86=B5=E7=9B=B4?= =?UTF-8?q?=E6=8E=A5=E4=BD=BF=E7=94=A8=E6=9D=A1=E4=BB=B6=E5=88=A4=E6=96=AD?= =?UTF-8?q?=E8=A1=A8=E8=BE=BE=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Validate.php | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/library/think/Validate.php b/library/think/Validate.php index 6b45ce11..7157b7d0 100644 --- a/library/think/Validate.php +++ b/library/think/Validate.php @@ -523,7 +523,7 @@ class Validate * 验证是否唯一 * @access protected * @param mixed $value 字段值 - * @param mixed $rule 验证规则 格式:数据表,字段名,排除ID + * @param mixed $rule 验证规则 格式:数据表,字段名,排除ID,主键名 * @param array $data 数据 * @param string $field 验证字段名 * @return bool @@ -534,13 +534,7 @@ class Validate $rule = explode(',', $rule); } $model = Loader::table($rule[0]); - $key = isset($rule[3]) ? $rule[3] : $model->getPk(); $field = isset($rule[1]) ? $rule[1] : $field; - if (isset($rule[2])) { - $except = $rule[2]; - } elseif (isset($data[$key])) { - $except = $data[$key]; - } if (strpos($field, '|')) { // 支持多个字段验证 @@ -554,9 +548,13 @@ class Validate $map[$field] = $data[$field]; } - if (isset($except)) { - $map[$key] = ['neq', $except]; + $key = strval(isset($rule[3]) ? $rule[3] : $model->getPk()); + if (isset($rule[2])) { + $map[$key] = ['neq', $rule[2]]; + } elseif (isset($data[$key])) { + $map[$key] = ['neq', $data[$key]]; } + if ($model->where($map)->field($key)->find()) { return false; }