多对多关联关系中sync()方法为什么只会更新一条记录?
举个例子
User表:
id | user_name |
---|---|
1 | John |
2 | Mike |
Car表:
id | car_name |
---|---|
3 | BMW |
4 | Benz |
5 | Audi |
中间表user_car_relation:
id | user_id | car_id | deleted_at |
---|---|---|---|
6 | 1 | 3 | NULL |
7 | 1 | 4 | NULL |
8 | 2 | 5 | NULL |
9 | 1 | 4 | NULL |
注意第 9 条记录,是与第7条重复的记录
从以上简单的关联关系可以看出,John 有两部车,分别是 BMW 和 Benz。Mike 只有一部车 Audi。基于以上数据,如果 John 卖掉了一辆 Benz 车,对应到代码中:
User::find(1)->sync([
4 => ['deleted_at' => date('Y-m-d H:i:s')],
], false);
此时user_car_relation
表会变为:
id | user_id | car_id | deleted_at |
---|---|---|---|
6 | 1 | 3 | NULL |
7 | 1 | 4 | 2021-06-19 12:32:39 |
8 | 2 | 5 | NULL |
9 | 1 | 4 | NULL |
看到没,第 9 条记录没有同步变化!
追溯到部分源码:
protected function updateExistingPivotUsingCustomClass($id, array $attributes, $touch)
{
$pivot = $this->getCurrentlyAttachedPivots()
->where($this->foreignPivotKey, $this->parent->{$this->parentKey})
->where($this->relatedPivotKey, $this->parseId($id))
// 这里为什么用first()只获取一条记录?如果我的关联关系表中出现多个重复的记录,就会导致重复的其他几条记录不能更新掉数据
->first();
}
问题
框架中sync()
方法只更新一条记录的用意是什么?这算BUG
吗?
推荐文章: