public function afterDelete()
    {
        foreach ($this->relationalFields as $attribute => $value) {
            $getter = 'get' . ucfirst($attribute);
            /** @var ActiveQuery $activeQuery */
            $activeQuery = $this->owner->$getter();

            $models = [];
            if (empty($activeQuery->via)) {
                $models = $activeQuery->all();
            } else {
                if ($activeQuery->via instanceof ActiveQueryInterface) { // viaTable

                    $junctionTable = $activeQuery->via->from[0];
                    list($junctionColumn) = array_keys($activeQuery->via->link);
                    list($relatedColumn) = array_values($activeQuery->link);

                    $rows = (new Query())
                        ->from($junctionTable)
                        ->select([
                            $junctionColumn,
                            $relatedColumn
                        ])
                        ->where([
                            $junctionColumn => $this->owner->getPrimaryKey(),
                        ])->all();

                    $this->relationsMap($rows, function($row) use ($junctionTable) {
                        Yii::$app->db->createCommand()
                            ->delete($junctionTable, $row)
                            ->execute();
                    });

                } else { // via
                    $junctionGetter = 'get' . ucfirst($activeQuery->via[0]);
                    $models = $this->owner->$junctionGetter()->all();
                }
            }

            foreach ($models as $model) {
                if (!$model->delete()) {
                    Yii::$app->getDb()->getTransaction()->rollBack();
                    throw new RelationException('Model ' . $model::className() . ' not deleted due to unknown error');
                }
            }
        }
    }