yii2-save-relations-behavior icon indicating copy to clipboard operation
yii2-save-relations-behavior copied to clipboard

Error Unable to link models: the primary key of

Open Sirgalas opened this issue 5 years ago • 0 comments

You have an error from 384 to 386 lines.

$p1 = $model->isPrimaryKey(array_keys ($relation->link));
$p2 = $relationModel::isPrimaryKey(array_values($relation->link));
if ($relationModel->getIsNewRecord () & & $p1 && !$p2) {
...

What's the point? With such a relations

    public function getCompany()
    {
        return $this->hasOne(Company:: className (), ['id' = > 'company_id']);
    } 

if the parent class has properties primarykey (in your example on the main page Project ) the same as the relation class (in your example on the main page Company ) then your behavior will work. But if in Project establish primaryKey as project_id the will error

Unable to link models: the primary key of ...

because relation class not save as for me it is more correct to write the following code

$p1 = $model->isPrimaryKey(array_values($relation->link));
$p2 = $relationModel:: isPrimaryKey(array_keys($relation->link));
if ($relationModel->getIsNewRecord() && !$p1 & & $p2) {

почему так для relation $this->hasOne(Company::className(), ['id' => 'company_id']);


array_keys($relation->link)='id'
array_values($relation->link)='company_id'

Sirgalas avatar Nov 13 '19 07:11 Sirgalas