Answer the question
In order to leave comments, you need to log in
Yii2 how to make a one to one relationship?
Hello.
Prompt how correctly to implement communication one to one.
There is a table of profiles, there is a table of users.
In the profile controller, I'm trying to get a complete user object with fields from User and Profile.
$profile = Profile::find()->select('*')->joinWith('user',true,'RIGHT JOIN')->where(['username'=>$login])->one();
Answer the question
In order to leave comments, you need to log in
In the user table, write the relationship:
public function getProfile() {
return $this->hasOne(Profile::class,['id'=>'id']);
}
$user = User::find()->andWhere(['username'=>$login])->one();
$user->name; // своятво пользователя
$user->profile->id // свойство профиля
$user = User::find()->with('profile')->andWhere(['username'=>$login])->one();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question