protected function saveModels($attribute)
    {
        $data = $this->relationalData[$attribute];

        /** @var ActiveRecord $model */
        foreach ($data['newModels'] as $model) {
            if ($model->isNewRecord) {
                if (!empty($data['activeQuery']->via)) {
                    // only for many-to-many
                    $junctionColumn = $data['junctionColumn'];
                    $model->$junctionColumn = $this->owner->getPrimaryKey();
                } elseif ($data['activeQuery']->multiple) {
                    // only one-to-many
                    foreach ($data['activeQuery']->link as $childAttribute => $parentAttribute) {
                        $model->$childAttribute = $this->owner->$parentAttribute;
                    }
                }
                if (!$model->save()) {
                    Yii::$app->getDb()->getTransaction()->rollBack();
                    throw new RelationException('Model ' . $model::className() . ' not saved due to unknown error');
                }
            }
        }

        // only for many-to-many
        $this->relationsMap($data['newRows'], function($row) use ($attribute, $data) {
            $junctionColumn = $data['junctionColumn'];
            $row[$junctionColumn] = $this->owner->getPrimaryKey();
            if (!$this->isExistingRow($row, $attribute)) {
                Yii::$app->db->createCommand()
                    ->insert($data['junctionTable'], $row)
                    ->execute();
            }
        });
    }