Skip to content

Commit 61fe1eb

Browse files
committed
修正多对多关联的detach方法
1 parent 641f10f commit 61fe1eb

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/model/relation/BelongsToMany.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
namespace think\model\relation;
1313

1414
use think\Collection;
15+
use think\Db;
1516
use think\db\Query;
1617
use think\Exception;
17-
use think\Db;
1818
use think\Model;
1919
use think\model\Pivot;
2020
use think\model\Relation;
@@ -69,13 +69,18 @@ public function pivot($pivot)
6969
/**
7070
* 实例化中间表模型
7171
* @param $data
72-
* @return mixed
72+
* @return Pivot
73+
* @throws Exception
7374
*/
7475
protected function newPivot($data = [])
7576
{
76-
$pivot = $this->pivotName ?: '\\think\\model\\Pivot';
77-
78-
return new $pivot($this->parent, $data, $this->middle);
77+
$class = $this->pivotName ?: '\\think\\model\\Pivot';
78+
$pivot = new $class($this->parent, $data, $this->middle);
79+
if ($pivot instanceof Pivot) {
80+
return $pivot;
81+
} else {
82+
throw new Exception('pivot model must extends: \think\model\Pivot');
83+
}
7984
}
8085

8186
/**
@@ -530,7 +535,7 @@ public function detach($data = null, $relationDel = false)
530535
$pivot[$this->localKey] = $this->parent->$pk;
531536

532537
if (isset($id)) {
533-
$pivot[$this->foreignKey] = is_array($id) ? ['in', $id] : $id;
538+
$pivot[$this->foreignKey] = is_array($id) ? [$this->foreignKey, 'in', $id] : [$this->foreignKey, '=', $id];
534539
}
535540

536541
$this->pivot->where($pivot)->delete();

0 commit comments

Comments
 (0)