M
M
Monitorkin2016-02-08 13:47:17
Yii
Monitorkin, 2016-02-08 13:47:17

(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
img.png
models name , UserOrg: name

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2016-02-08
@Monitorkin

In Service

public function getUser()
    {
        return $this->hasOne(User::className(),['id'=>'user_id']);
    }

In User:
public function getUserOrg()
    {
        return $this->hasOne(UserOrg::className(),['org'=>'id']);
    }

Conclusion:
$service = Service::findOne($id);
// можно так, меньше запросов к бд: 
// $service = Service::find()->with(['User','UserOrg'])->where(['id'=>$id])->one();
echo $service->user->username;
echo $servise->user->userOrg->name;

Everything is here: www.yiiframework.com/doc-2.0/guide-db-active-recor...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question