A
A
Andrey Kotosin2018-06-11 11:30:53
Yii
Andrey Kotosin, 2018-06-11 11:30:53

How to link tables in widget yii2?

There is a left menu widget in yii2 which is made like this:
components/LeftbarWidget.php

<?php

namespace app\components;
use yii\base\Widget;
use Yii;

class LeftbarWidget extends Widget {

    public function run() {
        $id = Yii::$app->request->get('id');
        $users = \dektrium\user\models\User::findOne($id);
        return $this->render('leftbar_tpl', compact('users'));
// Здесь я получаю и передаю в html структуру виджета данные о пользователе
    }
}

In leftbar_tpl.php, I want not only to transfer data from the user table, but also to make a connection between tables, for example, user and setting (in setting, by the key user_id, I have data about user settings)
How can I implement all this? I do not quite understand how not in the model, but in the widget, to connect 2 or more tables.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Kotosin, 2018-06-11
@av_kotosin

I did it as follows:
In the widget in run (), I wrote something like this:
In the widget, I ran it through foreach and displayed the data.
In the same model, Contact established a connection. thanks to all)

M
Maxim Timofeev, 2018-06-12
@webinar

The widget should not query the database. It should build html based on the data passed to it. So you have ready-made data should come to the widget.
This is:
should be in the controller, and if you need it everywhere, you can put it in the config, for example:

'on beforeAction' => function($event){
Yii::$app->params['user'] = \dektrium\user\models\User::findOne(Yii::$app->request->get('id'));
}

and where you call the widget, pass to itYii::$app->params['user']

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question