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

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

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