Answer the question
In order to leave comments, you need to log in
How to pass model to yii2 custom widget?
Good afternoon.
You need to display data from several models on the main page, in the footer.
The widget was written, but the question arose, how now to dynamically transfer the model to the widget class?
Now it looks like this:
// вызов виджета на главной
<?= MainWidget::widget(['limit' => 7]); ?>
// класс виджета
class MainWidget extends Widget
{
public $limit; // LIMIT на количество записей из базы
public $data; // Сюда надо передать модель
public function init()
{
if($this->limit == null){
$this->limit = 5;
}
parent::init();
}
public function run()
{
$listGlobe = Countries::find()->where(['status' => Countries::STATUS_ACTIVE])->limit($this->limit)->all();
return $this->render('index', ['listGlobe' => $this->data]);
}
}
<?php
$listGlobe = NameModel::find()->where(['status' => Countries::STATUS_ACTIVE])->limit($this->limit)->all();
?>
<?php
GlobeWidget::widget(['data' => NameModel]);
?>
Answer the question
In order to leave comments, you need to log in
For example like this:
<?php
$model = MyModel::find()->all();
echo MainWidget::widget(['limit' => 7, 'data'=>$model]); ?>
<?php
echo MainWidget::widget(['limit' => 7, 'data'=>MyModel::classname]); ?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question