public function saveData()
    {
        $needSaveOwner = false;

        foreach ($this->relationalData as $attribute => $data) {
            // delete models
            $this->deleteModels($attribute);
            // save models
            $this->saveModels($attribute);

            if (!$data['activeQuery']->multiple && (count($data['newModels']) == 0 || !$data['newModels'][0]->isNewRecord)) {
                $needSaveOwner = true;
                foreach ($data['activeQuery']->link as $childAttribute => $parentAttribute) {
                    $this->owner->$parentAttribute = count($data['newModels']) ? $data['newModels'][0]->$childAttribute : null;
                }
            }
        }

        $this->relationalFinished = true;

        if ($needSaveOwner) {
            $model = $this->owner;
            $this->detach();
            if (!$model->save()) {
                Yii::$app->getDb()->getTransaction()->rollBack();
                throw new RelationException('Owner-model ' . $model::className() . ' not saved due to unknown error');
            }
        }
    }