/** * @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; }
/** * @param \Dja\Db\Model\Model $model * @return \Dja\Db\Model\Query\QuerySet */ public function getRelation(\Dja\Db\Model\Model $model) { /** @var \Dja\Db\Model\Model $relationClass */ $relationClass = $this->relationClass; /** @var \Dja\Db\Model\Model $throughClass */ $throughClass = $this->throughClass; if ($throughClass) { $in = $throughClass::objects()->filter([$this->self_field => $model->__get($this->self_field)])->valuesList($this->to_field, false); } else { $sql = sprintf('SELECT "%s" FROM "%s" WHERE "%s" = %d', $this->to_field, $this->db_table, $this->self_field, $model->__get($this->self_field)); //$in = $relationClass::objects()->setRawSql($sql)->valuesDict($this->to_field, $this->to_field); $in = Expr($sql); } if ($this->limit_choices_to) { $filter = $this->limit_choices_to; $filter[$this->to_field . '__in'] = $in; return $relationClass::objects()->relation($model, $this)->filter($filter); } else { return $relationClass::objects()->relation($model, $this)->filter([$this->to_field . '__in' => $in]); } }
/** * dispatch global and local model events * no event objects created if no listeners attached * return false if some event stop propagation (means u need to cancel dependent actions) * @param string $eventName * @return bool */ protected function eventDispatch($eventName) { if (Model::events()->hasListeners($eventName)) { $e = new Event($this); Model::events()->dispatch($eventName, $e); if ($e->isPropagationStopped()) { return false; } } if ($this->metadata->events()->hasListeners($eventName)) { if (!isset($e)) { $e = new Event($this); } $this->metadata->events()->dispatch($eventName, $e); if ($e->isPropagationStopped()) { return false; } } return true; }
/** * @param \Dja\Db\Model\Model $model * @return \Dja\Db\Model\Model */ public function getRelation(\Dja\Db\Model\Model $model) { /** @var \Dja\Db\Model\Model $relationClass */ $relationClass = $this->relationClass; $value = $model->__get($this->db_column); if (!empty($value)) { $inst = $relationClass::objects()->filter([$this->to_field => (int) $value])->current(); return $inst; } else { return $this->default; } }