public function createM2MIntermediaryModel($field, $model)
    {
        $modelName = $model->getMeta()->getNSModelName();

        if (is_string($field->relation->toModel)) {
            $toModelName = Tools::resolveRelation(
                $model,
                $field->relation->toModel
            );
            $ref = new \ReflectionClass($toModelName);
            $toModelName = $ref->getShortName();
            $toNamespacedModelName = $ref->getName();
        } else {
            $toModelName = $field->relation->toModel->getMeta()->getModelName();
            $toNamespacedModelName = $field->relation->toModel
                ->getMeta()->getNSModelName();
        }

        $className = sprintf(
            '%s_%s_autogen',
            $model->getMeta()->getModelName(),
            $field->getName()
        );
        $from = strtolower($model->getMeta()->getModelName());
        $to = strtolower($toModelName);
        if ($from == $to) {
            $to = sprintf('to_%s', $to);
            $from = sprintf('from_%s', $from);
        }
        $fields = [
            $from => ForeignKey::createObject(
                [
                    'to' => $modelName,
                    'relatedName' => sprintf('%s+', $className),
                    'dbConstraint' => $field->relation->dbConstraint,
                    'onDelete' => Delete::CASCADE,
                ]
            ),
            $to => ForeignKey::createObject(
                [
                    'to' => $toNamespacedModelName,
                    'relatedName' => sprintf('%s+', $className),
                    'dbConstraint' => $field->relation->dbConstraint,
                    'onDelete' => Delete::CASCADE,
                ]
            ),
        ];

        /* @var $intermediaryObj Model */
        $intermediaryClass = FormatFileContent::modelFileTemplate(
            $model->getMeta()->getModelNamespace(), $className, Model::class);

        $fullname = sprintf("%s\%s", $model->getMeta()->getModelNamespace(), $className);
        if (!class_exists($fullname, false)) {
            eval($intermediaryClass->toString());
        }

        /** @var $obj Model */
        $obj = new $fullname(['registry' => $model->getMeta()->getRegistry()]);

        $obj->setupClassInfo(
            $fields,
            [
                'meta' => [
                    'appName' => $model->getMeta()->getAppName(),
                    'dbTable' => $field->getM2MDbTable($model->getMeta()),
                    'verboseName' => sprintf('%s-%s relationship', $from, $to),
                    'uniqueTogether' => [$from, $to],
                    'autoCreated' => true,
                ],
                'registry' => $model->getMeta()->getRegistry(),
            ]
        );

        return $obj;
    }