From 26380b7019b40df814f06f1799ec4500fb38e0c9 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 25 Oct 2018 12:34:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=9A=E5=AF=B9=E5=A4=9A=E5=85=B3=E8=81=94?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=8C=87=E5=AE=9A=E4=B8=AD=E9=97=B4=E8=A1=A8?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=90=8D=E7=A7=B0=20after/before=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E6=94=AF=E6=8C=81=E6=8C=87=E5=AE=9A=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Validate.php | 14 +++++++++----- library/think/model/relation/BelongsToMany.php | 18 ++++++++++++++++-- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/library/think/Validate.php b/library/think/Validate.php index 0b97e64d..73412b7c 100644 --- a/library/think/Validate.php +++ b/library/think/Validate.php @@ -315,8 +315,8 @@ class Validate // 字段验证 if ($rule instanceof \Closure) { - // 匿名函数验证 - $result = call_user_func_array($rule, [$value, $data, $title, $msg, $this]); + // 匿名函数验证 支持传入当前字段和所有字段两个数据 + $result = call_user_func_array($rule, [$value, $data]); } else { $result = $this->checkItem($key, $value, $rule, $data, $title, $msg); } @@ -1113,10 +1113,12 @@ class Validate * @access protected * @param mixed $value 字段值 * @param mixed $rule 验证规则 + * @param array $data 数据 * @return bool */ - protected function after($value, $rule) + protected function after($value, $rule, $data) { + $rule = $this->getDataValue($data, $rule); return strtotime($value) >= strtotime($rule); } @@ -1125,10 +1127,12 @@ class Validate * @access protected * @param mixed $value 字段值 * @param mixed $rule 验证规则 + * @param array $data 数据 * @return bool */ - protected function before($value, $rule) + protected function before($value, $rule, $data) { + $rule = $this->getDataValue($data, $rule); return strtotime($value) <= strtotime($rule); } @@ -1247,7 +1251,7 @@ class Validate list($name1, $name2) = explode('.', $key); $value = isset($data[$name1][$name2]) ? $data[$name1][$name2] : null; } else { - $value = isset($data[$key]) ? $data[$key] : null; + $value = isset($data[$key]) ? $data[$key] : $key; } return $value; } diff --git a/library/think/model/relation/BelongsToMany.php b/library/think/model/relation/BelongsToMany.php index 787edc07..64629c0f 100644 --- a/library/think/model/relation/BelongsToMany.php +++ b/library/think/model/relation/BelongsToMany.php @@ -29,6 +29,8 @@ class BelongsToMany extends Relation protected $pivotName; // 中间表模型对象 protected $pivot; + // 中间表数据名称 + protected $pivotDataName = 'pivot'; /** * 构造函数 @@ -70,6 +72,18 @@ class BelongsToMany extends Relation return $this; } + /** + * 设置中间表数据名称 + * @access public + * @param string $name + * @return $this + */ + public function pivotDataName($name) + { + $this->pivotDataName = $name; + return $this; + } + /** * 获取中间表更新条件 * @param $data @@ -118,7 +132,7 @@ class BelongsToMany extends Relation } } } - $model->setRelation('pivot', $this->newPivot($pivot, true)); + $model->setRelation($this->pivotDataName, $this->newPivot($pivot, true)); } } @@ -384,7 +398,7 @@ class BelongsToMany extends Relation } } } - $set->setRelation('pivot', $this->newPivot($pivot, true)); + $set->setRelation($this->pivotDataName, $this->newPivot($pivot, true)); $data[$pivot[$this->localKey]][] = $set; } return $data;