belongsToMany增加数据同步方法

This commit is contained in:
yunwuxin
2017-03-23 17:45:15 +08:00
parent 0ee05eb1ca
commit 05be8d9664

View File

@@ -121,7 +121,7 @@ class BelongsToMany extends Relation
public function getRelation($subRelation = '', $closure = null)
{
if ($closure) {
call_user_func_array($closure, [ & $this->query]);
call_user_func_array($closure, [& $this->query]);
}
$result = $this->buildQuery()->relation($subRelation)->select();
$this->hydratePivot($result);
@@ -503,6 +503,55 @@ class BelongsToMany extends Relation
}
}
/**
* 数据同步
* @param array $ids
* @param bool $detaching
* @return array
*/
public function sync($ids, $detaching = true)
{
$changes = [
'attached' => [],
'detached' => [],
'updated' => []
];
$pk = $this->parent->getPk();
$current = $this->pivot->where($this->localKey, $this->parent->$pk)
->column($this->foreignKey);
$records = [];
foreach ($ids as $key => $value) {
if (!is_array($value)) {
$records[$value] = [];
} else {
$records[$key] = $value;
}
}
$detach = array_diff($current, array_keys($records));
if ($detaching && count($detach) > 0) {
$this->detach($detach);
$changes['detached'] = $detach;
}
foreach ($records as $id => $attributes) {
if (!in_array($id, $current)) {
$this->attach($id, $attributes);
$changes['attached'][] = $id;
} elseif (count($attributes) > 0 &&
$this->attach($id, $attributes)
) {
$changes['updated'][] = $id;
}
}
return $changes;
}
/**
* 执行基础查询(进执行一次)
* @access protected