{"record":{"id":"28da343ae485dd35","repo":"yiisoft/yii2","slug":"unable-to-link-models-the-models-being-linked-can","errorCode":null,"errorMessage":"Unable to link models: the models being linked cannot be newly created.","messagePattern":"Unable to link models: the models being linked cannot be newly created\\.","errorType":"exception","errorClass":"yii\\base\\InvalidCallException","httpStatus":null,"severity":"error","filePath":"framework/db/BaseActiveRecord.php","lineNumber":1318,"sourceCode":"     * junction table which contains the primary key values from both models.\n     *\n     * Note that this method requires that the primary key value is not null.\n     *\n     * @param string $name the case sensitive name of the relationship, e.g. `orders` for a relation defined via `getOrders()` method.\n     * @param ActiveRecordInterface $model the model to be linked with the current one.\n     * @param array $extraColumns additional column values to be saved into the junction table.\n     * This parameter is only meaningful for a relationship involving a junction table\n     * (i.e., a relation set with [[ActiveRelationTrait::via()]] or [[ActiveQuery::viaTable()]].)\n     * @throws InvalidCallException if the method is unable to link two models.\n     */\n    public function link($name, $model, $extraColumns = [])\n    {\n        /** @var ActiveQueryInterface|ActiveQuery $relation */\n        $relation = $this->getRelation($name);\n\n        if ($relation->via !== null) {\n            if ($this->getIsNewRecord() || $model->getIsNewRecord()) {\n                throw new InvalidCallException('Unable to link models: the models being linked cannot be newly created.');\n            }\n            if (is_array($relation->via)) {\n                /** @var ActiveQuery $viaRelation */\n                list($viaName, $viaRelation) = $relation->via;\n                $viaClass = $viaRelation->modelClass;\n                // unset $viaName so that it can be reloaded to reflect the change\n                unset($this->_related[$viaName]);\n            } else {\n                $viaRelation = $relation->via;\n                $viaTable = reset($relation->via->from);\n            }\n            $columns = [];\n            foreach ($viaRelation->link as $a => $b) {\n                $columns[$a] = $this->$b;\n            }\n            foreach ($relation->link as $a => $b) {\n                $columns[$b] = $model->$a;\n            }","sourceCodeStart":1300,"sourceCodeEnd":1336,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/db/BaseActiveRecord.php#L1300-L1336","documentation":"Error \"Unable to link models: the models being linked cannot be newly created.\" thrown in yiisoft/yii2.","triggerScenarios":"Thrown at framework/db/BaseActiveRecord.php:1318 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"66f00d18a29b520f85e8e8f1e32d1e7e7b556cac","analyzedAt":"2026-08-17T05:17:23.470Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}