Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.

Fail when parent model has more than one PRIMARY KEY #61

Open
andrey-denisenko opened this issue Jun 16, 2020 · 0 comments
Open

Fail when parent model has more than one PRIMARY KEY #61

andrey-denisenko opened this issue Jun 16, 2020 · 0 comments
Assignees

Comments

@andrey-denisenko
Copy link

Hello,

In short words:
If my model has more than one PK like 'id' and 'timestamp'.
Then your code is failing here:


    private function _prepareHasOneRelation(BaseActiveRecord $model, $relationName, ModelEvent $event)
    {
        Yii::debug("_prepareHasOneRelation for {$relationName}", __METHOD__);
        $relationModel = $model->{$relationName};
        $this->validateRelationModel(self::prettyRelationName($relationName), $relationName, $model->{$relationName});
        $relation = $model->getRelation($relationName);
        $p1 = $model->isPrimaryKey(array_keys($relation->link)); <<<<HERE only 'id' is passed to isPrimaryKey()
        $p2 = $relationModel::isPrimaryKey(array_values($relation->link));

        if ($relationModel->getIsNewRecord() && $p1 && !$p2) {
            // Save Has one relation new record
            if ($event->isValid && (count($model->dirtyAttributes) || $model->{$relationName}->isNewRecord)) {
                Yii::debug('Saving ' . self::prettyRelationName($relationName) . ' relation model', __METHOD__);
                if ($model->{$relationName}->save()) {
                    $this->_savedHasOneModels[] = $model->{$relationName};
                }
            }
        }
    }

But frankly speaking I'm not sure that bug on your side.
Thats why I've rised same question on YII2 project
And where I described my problem in all details.

If I where you, I would avoid usage of function BaseActiveRecord::isPrimaryKey()
And use your own implementaion instead.

@andrey-denisenko andrey-denisenko changed the title Fail when parent model has more than on PRIMARY KEY Fail when parent model has more than one PRIMARY KEY Jun 16, 2020
@nstCactus nstCactus self-assigned this Mar 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants