Answer the question
In order to leave comments, you need to log in
(Yii2) How to display data from three related tables?
There are 3 tables:
Service (user services) User
( user data)
UserOrg (organization in which the user works) and
related
models name , UserOrg: name
Answer the question
In order to leave comments, you need to log in
In Service
public function getUser()
{
return $this->hasOne(User::className(),['id'=>'user_id']);
}
public function getUserOrg()
{
return $this->hasOne(UserOrg::className(),['org'=>'id']);
}
$service = Service::findOne($id);
// можно так, меньше запросов к бд:
// $service = Service::find()->with(['User','UserOrg'])->where(['id'=>$id])->one();
echo $service->user->username;
echo $servise->user->userOrg->name;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question