Yii2 how to get nested svzi and to count them?

Make statistics to the user.
For days, weeks, months. year. all time and date range.

There are three entities who tied all of them glued one-to-many
In the table coupon is partner_id
The table order is partner_id, —Āoupon_id
Everywhere there are the creation date in UNIX from TimeStampBehavior

on the controller for every period created of action. On the front is just a button.

For the entire period I did the following. received id of the current partner and pulled all the coupons belonging to it. put it in the data provider, and on frontend. I have a second request countem.
 'itemView' => '_coupons',
 ]) ?>
 <table class="table table-hover">
 <td><strong>coupon Name</strong></td>
 <?= \yii\widgets\ListView::widget([
 'dataProvider' =--></tbody></table>

directly counter.
use yii\helpers\Html;
use yii\helpers\Markdown;

//get number of
$orders = \common\models\Order::find()--->where(['coupon_id' => $model->id])->count(); ?>

 <?= Html::encode($model--->title) ?>
 <?= Html::encode($orders) ?>

Actually the dates I count lay terms by date. And before that date convert in this way
$timestamp = time();
 $dates = Yii::app()->dateFormatter->format('dd-MM-yyyy', $timestamp);
 $date = new DateTime($dates);
 $date->modify('-1 day');
 $date_elements = explode("-",$date->format('d-my'));
 $beforeTime = mktime(0,0,0,$date_elements[1],$date_elements[0],$date_elements[2]);

And it turns out that the +1 request on each coupon. The coupons have the status and statistics archive none. In fact, more than 10 product coupons will not. But still, written by the crutches and I don't know how to improve them through service provider or as all the statistics would be more correct to implement? Given now that there are links, but to use them because I can't get a partner then its a bunch of coupons, and then all orders for these coupons.
July 8th 19 at 11:47
1 answer
July 8th 19 at 11:49
as far as I remember, yii was a special type of link called a static query. this special from linking just meant for queries of aggregation. second yii think this also should be
The second was removed, but nothing prevents to implement the method yourself. - Allen.Harb commented on July 8th 19 at 11:52
: googling.. working on the ground, never heard about it. - heber_Prohas commented on July 8th 19 at 11:55

Find more questions by tags Yii