N
N
nnkrasovok2019-04-17 10:47:45
Yii
nnkrasovok, 2019-04-17 10:47:45

How to select user from related table with condition (hasOne)?

in this entry writes the user id from the user table to the user_id field of the profile table.

public function getUser()
    {
        return $this->hasOne($this->module->manager->userClass, ['id' => 'user_id']);
    }

how do I make it check the role_id field of the user table and if the value is 2 then only then does the write happen?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
davidnum95, 2019-04-17
@nnkrasovok

public function afterSave($insert, $changedAttributes)
    {
        if ($insert && $this->role_id === 2) {
            $profile = $this->module->manager->createProfile([
                'user_id'        => $this->id,
                'gravatar_email' => $this->email
            ]);
            $profile->save(false);
        }
        parent::afterSave($insert, $changedAttributes);
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question