/** * @param Model $model * @return $this * @throws \InvalidArgumentException * @throws \BadMethodCallException */ public function remove($model) { /** @var Model[] $list */ if ($model instanceof QuerySet) { $list = $model; } else { $list = func_get_args(); } if ($this->ownerField instanceof SingleRelation) { foreach ($list as $model) { if ($model->isNewRecord()) { throw new \InvalidArgumentException('Cant remove record that is not saved'); } $model->__unset($this->ownerField->to_field); $model->save(); } } elseif ($this->ownerField instanceof ManyToManyRelation) { if ($this->ownerField->throughClass) { $throughClass = $this->ownerField->throughClass; $in = []; foreach ($list as $model) { if ($model->isNewRecord()) { throw new \InvalidArgumentException('Cant remove record that is not saved'); } $in[] = $model->__get($this->ownerField->to_field); } foreach ($throughClass::objects()->filter([$this->ownerField->to_field . '__in' => $in]) as $link) { $link->delete(); } } } else { throw new \BadMethodCallException('Cant modify query set for field ' . $this->ownerField->name); } //$this->resetStatement(); return $this; }