mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 12:42:48 +08:00
改进多对多关联的中间表模型更新
This commit is contained in:
@@ -79,22 +79,23 @@ class BelongsToMany extends Relation
|
||||
{
|
||||
return [
|
||||
$this->localKey => $data[$this->localKey],
|
||||
$this->foreignKey => $data[$this->foreignKey]
|
||||
$this->foreignKey => $data[$this->foreignKey],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 实例化中间表模型
|
||||
* @param $data
|
||||
* @param array $data
|
||||
* @param bool $isUpdate
|
||||
* @return Pivot
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function newPivot($data = [])
|
||||
protected function newPivot($data = [], $isUpdate = false)
|
||||
{
|
||||
$class = $this->pivotName ?: '\\think\\model\\Pivot';
|
||||
$pivot = new $class($data, $this->parent, $this->middle);
|
||||
if ($pivot instanceof Pivot) {
|
||||
return $pivot;
|
||||
return $isUpdate ? $pivot->isUpdate(true, $this->getUpdateWhere($data)) : $pivot;
|
||||
} else {
|
||||
throw new Exception('pivot model must extends: \think\model\Pivot');
|
||||
}
|
||||
@@ -117,7 +118,7 @@ class BelongsToMany extends Relation
|
||||
}
|
||||
}
|
||||
}
|
||||
$model->setRelation('pivot', $this->newPivot($pivot)->isUpdate(true, $this->getUpdateWhere($pivot)));
|
||||
$model->setRelation('pivot', $this->newPivot($pivot, true));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -383,7 +384,7 @@ class BelongsToMany extends Relation
|
||||
}
|
||||
}
|
||||
}
|
||||
$set->setRelation('pivot', $this->newPivot($pivot)->isUpdate(true, $this->getUpdateWhere($pivot)));
|
||||
$set->setRelation('pivot', $this->newPivot($pivot, true));
|
||||
$data[$pivot[$this->localKey]][] = $set;
|
||||
}
|
||||
return $data;
|
||||
@@ -486,7 +487,7 @@ class BelongsToMany extends Relation
|
||||
foreach ($ids as $id) {
|
||||
$pivot[$this->foreignKey] = $id;
|
||||
$this->pivot->insert($pivot, true);
|
||||
$result[] = $this->newPivot($pivot)->isUpdate(true, $this->getUpdateWhere($pivot));
|
||||
$result[] = $this->newPivot($pivot, true);
|
||||
}
|
||||
if (count($result) == 1) {
|
||||
// 返回中间表模型对象
|
||||
|
||||
Reference in New Issue
Block a user