改进BelongsToMany类的saveall方法 增加第三个参数 用于指定额外参数是否一致

This commit is contained in:
thinkphp
2016-12-22 18:12:33 +08:00
parent c2fff2b45d
commit 340abd05e5

View File

@@ -231,13 +231,17 @@ class BelongsToMany extends Relation
* @access public * @access public
* @param array $dataSet 数据集 * @param array $dataSet 数据集
* @param array $pivot 中间表额外数据 * @param array $pivot 中间表额外数据
* @param bool $samePivot 额外数据是否相同
* @return integer * @return integer
*/ */
public function saveAll(array $dataSet, array $pivot = []) public function saveAll(array $dataSet, array $pivot = [], $samePivot = false)
{ {
$result = false; $result = false;
foreach ($dataSet as $key => $data) { foreach ($dataSet as $key => $data) {
$result = $this->attach($data, !empty($pivot) ? $pivot[$key] : []); if (!$samePivot) {
$pivot = !empty($pivot) ? $pivot[$key] : [];
}
$result = $this->attach($data, $pivot);
} }
return $result; return $result;
} }