Answer the question
In order to leave comments, you need to log in
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 структуру виджета данные о пользователе
}
}
Answer the question
In order to leave comments, you need to log in
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)
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'));
}
Yii::$app->params['user']
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question