How to pass the model to a custom widget in yii2?

Good afternoon.
Need to get data from multiple models on the main page, in the footer.
Widget wrote, but the question arose now as to dynamically pass the model to the widget class?
Now looks like this:
// call widget to your home
<?= MainWidget::widget(['limit' =--> 7]); ?>

// the widget class
class MainWidget extends Widget
{
 public $limit; // LIMIT on number of records from the database
 public $data; // Here need to pass the model

 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]);
}
}


Can't understand how public $data to pass the model from the outside, not to write for each model
<?php
$listGlobe = NameModel::find()--->where(['status' => Countries::STATUS_ACTIVE])->limit($this->limit)->all();
?>

but to call a widget to specify
<?php
GlobeWidget::widget(['data' =--> NameModel]);
?>
July 2nd 19 at 17:17
2 answers
July 2nd 19 at 17:19
Solution
For example:
<?php
$model = MyModel::find()--->all();
echo MainWidget::widget(['limit' => 7, 'data'=>$model]); ?>

You can of course, and the class name to pass in the model, but this is not correct:
<?php
echo MainWidget::widget(['limit' =--> 7, 'data'=>MyModel::classname]); ?>

Ideally, the model should occur in the controller and passed to view where next to be passed to the widget. Or widget should not get the model itself and particular data. Maybe You have to write your own dataProvider. look at the example of activedataprovider www.yiiframework.com/doc-2.0/guide-output-data-pro...
Here as would like to avoid creating controller models and to transfer to the view -- Or the same type of data with active status and a limit on the sample.
Well, okay, if not, then not. Will do strictly according to the documentation.
The DataProvider and not needed in this case. It should just display the data without sorting, splitting by pages and other frills. The usual array to loop through and display the ul listing.
I thought about ListView, but again - the DataProvider, and necessary, again, it is not. - Dion77 commented on July 2nd 19 at 17:22
And if you do so, the controller to collect all models into a single array using a custom method, and in one fell swoop to pass in a widget?
An array of such a plan:
[
 'model1' => [
'data1',
'data2',
'data3'
],
 'model2' => [
'data1',
'data2',
'data3'
],
 'model3' => [
'data1',
'data2',
'data3'
 ], 
]

But in the widget to loop through the array and display the data. - Dion77 commented on July 2nd 19 at 17:25
: so the DataProvider just for this, just make your in which all the logic that You don't like the controller. - Henderson_Beat commented on July 2nd 19 at 17:28
: Here's what I can't figure out. These data need to output to all actions and controllers. To create in each controller for the data it is stressful.
That's where the data forming in such a case? - Dion77 commented on July 2nd 19 at 17:31
: Can be to make a component? To generate the data to cache and check whether they are in the cache. To access a method of a component that will return the correct dataset. Something like this:
<?php
echo MainWidget::widget(['limit' =--> 7, 'data'=>Yii::$app->myDataBuilder->get()); 
?>
- Henderson_Beat commented on July 2nd 19 at 17:34
: Perhaps this option will suit. What kind of menu you need to withdraw. - Dion77 commented on July 2nd 19 at 17:37
: Thank you for the tips. - Dion77 commented on July 2nd 19 at 17:40
Something happened, but I'm not sure that doing the right thing. How You can be contacted in skype? If You don't mind. - Dion77 commented on July 2nd 19 at 17:43
: my skype: inet_shop1 loaded, so the answer is not immediately promise, but I will answer - Henderson_Beat commented on July 2nd 19 at 17:46
July 2nd 19 at 17:21
Solution
Use widgets for the logic output and not to receive data. Data is retrieved in the controller.
Your widget has no logic, so you can directly transfer data in a view widget.

Not to create each time the same type of request, it is possible to customize ActiveQuery and do something like this
Countries::find()->findActiveAll($limit);
Towns::find()->findActiveAll($limit);
Thank you for the tips. - Dion77 commented on July 2nd 19 at 17:24

Find more questions by tags Yii