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

        $activeQuery = $data['activeQuery'];
        /** @var ActiveRecord $class */
        $class = $activeQuery->modelClass;

        $via = $activeQuery->via;
        $data['junctionTable'] = $via->from[0];

        list($data['junctionColumn']) = array_keys($via->link);
        list($data['relatedColumn']) = array_values($activeQuery->link);
        $junctionColumn = $data['junctionColumn'];
        $relatedColumn = $data['relatedColumn'];

        if (!empty($data['data'])) {
            // make sure what all row's ids from POST exists in database
            $countManyToManyModels = $class::find()->where([$class::primaryKey()[0] => $data['data']])->count();
            if ($countManyToManyModels != count($data['data'])) {
                throw new RelationException('Related records for attribute ' . $attribute . ' not found');
            }
            // create new junction rows
            foreach ($data['data'] as $relatedModelId) {
                $junctionModel = array_merge(
                    !ArrayHelper::isAssociative($via->on) ? [] : $via->on,
                    [$junctionColumn => $this->owner->getPrimaryKey()]
                );
                $junctionModel[$relatedColumn] = $relatedModelId;
                if (isset($this->relations[$attribute]) && is_callable($this->relations[$attribute])) {
                    $junctionModel = call_user_func($this->relations[$attribute], $junctionModel, $relatedModelId);
                }
                $data['newRows'][] = $junctionModel;
            }
        }

        if (!empty($this->owner->getPrimaryKey())) {
            $data['oldRows'] = (new Query())
                ->from($data['junctionTable'])
                ->select(array_merge(
                        [$junctionColumn, $relatedColumn],
                        !ArrayHelper::isAssociative($via->on) ? [] : array_keys($via->on))
                )->where(array_merge(
                    !ArrayHelper::isAssociative($via->on) ? [] : $via->on,
                    [$junctionColumn => $this->owner->getPrimaryKey()]
                ))->all();
        }

        $this->relationalData[$attribute] = $data;
    }