Answer the question
In order to leave comments, you need to log in
Yii2 Widgets and database?
Good day, I'm very young and green in php and in yii in particular, I'm interested in the question of whether it is correct to connect to the database table in the widget. If it's not correct, I ask you to explain, if possible, how to correctly display data in the widget.
For clarity, in this way, in this menu there will be pictures that I would like to replace as needed, I don’t know other ways how to pull pictures from the database into the menu (let’s say on the same bg) and subsequently change them as you please.
$menu = Imagemenumain::find()->all();
return $this->render
('myimagemenu',
['menuimg' => $menu]
);
}
Answer the question
In order to leave comments, you need to log in
Here, probably, any answer will be controversial, but I think it’s better to do this:
in the config (or in the base controller) we cling to the beforeAction event:
'on beforeAction' => function($event){
Yii::$app->params['menuData'] = ArrayHelper::map(Imagemenumain::find()->all(),'id','title');
//ArrayHelper тут для примера, хотел обозначить, что не стоит засовывать в params более чем нужно виджету. Там должен оказаться подготовленный массив с нужными данными
},
echo SomeMyWidget::widget([
'someWidgetParam' => Yii::$app->params['menuData'],
'someWidgetParam2' => 'маракуя',
'otmetitOtvetom' => true
]);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question