/** * Handles afterInsert event of the owner. */ public function afterInsert() { if ($this->_model) { if ($this->ownerType) { $this->_model->entity_type = $this->ownerType; } $this->owner->link('metaRelation', $this->_model); } }
/** * @param array $files */ protected function saveFilesToRelation($files) { $modelClass = $this->getUploadModelClass(); foreach ($files as $file) { $model = new $modelClass(); $model->setScenario($this->uploadModelScenario); $model = $this->loadModel($model, $file); if ($this->getUploadRelation()->via !== null) { $model->save(false); } $this->owner->link($this->uploadRelation, $model); } }
public function afterSave() { $data = []; if (isset($_POST[$this->owner->formName()])) { $data = $_POST[$this->owner->formName()]; } if ($data) { foreach ($data as $attribute => $value) { if (!in_array($attribute, $this->relations)) { continue; } if (is_array($value)) { $relation = $this->owner->getRelation($attribute); if ($relation->via !== null) { /** @var ActiveRecord $foreignModel */ $foreignModel = $relation->modelClass; $this->owner->unlinkAll($attribute, true); if (is_array(current($value))) { foreach ($value as $data) { if (isset($data[$foreignModel::primaryKey()[0]]) && $data[$foreignModel::primaryKey()[0]] > 0) { $fm = $foreignModel::findOne($data[$foreignModel::primaryKey()[0]]); $fm->load($data, ''); $this->owner->link($attribute, $fm); } } } else { foreach ($value as $fk) { if (preg_match('~^\\d+$~', $fk)) { $this->owner->link($attribute, $foreignModel::findOne($fk)); } } } } } else { $this->owner->unlinkAll($attribute, true); } } } }
public static function linkAll(ActiveRecord $model, array $models, $relationName) { try { foreach ($models as $item) { if ($item->isNewRecord) { $model->link($relationName, $item); } else { $item->save(); } } return true; } catch (Exception $e) { return false; } }
/** * Saves a single model within a relation. * via: item is saved, then linked * direct link: item is saved by linking * existing models: * @param ActiveRecord $model base model * @param string $relationName name of the relation * @param boolean $isVia relation via table * @param ActiveRecord $item the model in the relation * @param array $config configuration array * @return boolean save success * @internal */ private function saveRelationModel(&$model, $relationName, $isVia, &$item, &$config) { $result = true; $skipUpdate = ArrayHelper::getValue($config, self::SKIP_UPDATE, $this->defaultSkipUpdate); if ($isVia) { if ($item->isNewRecord) { $result = $result && $item->save(); $model->link($relationName, $item); } else { if (!$skipUpdate) { $result = $result && $item->save(); } $findInRelation = $model->getRelation($relationName)->andWhere($item->getPrimaryKey(true))->exists(); if (!$findInRelation) { $model->link($relationName, $item); } } } else { if ($item->isNewRecord) { $model->link($relationName, $item); } else { if (!$skipUpdate) { // save linked item only, if something changed if (count($item->getDirtyAttributes()) > 0) { $item->save(); } } } } $result = $result && $this->saveRelations($item, $config); return $result; }