多对多关联支持指定中间表数据名称

after/before验证支持指定字段验证
This commit is contained in:
thinkphp
2018-10-25 12:34:51 +08:00
parent d77e233c45
commit 26380b7019
2 changed files with 25 additions and 7 deletions

View File

@@ -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;
}

View File

@@ -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;