$gallery = \common\models\UtVideogallery::find()->select(['ut_videogallery.VideogalleryID',
                'ut_videogallery.Name_rus','ut_videogallery.TournamentID','ut_videogallery.MatchID', 'Slug',
                'ut_videogallery.Date','COUNT(ut_video_to_gallery.VideoToGalleryID) count','ut_matches_videos.Data',
                'm1.Data DataP'])
                ->leftJoin('ut_video_to_gallery','ut_video_to_gallery.VideogalleryID=ut_videogallery.VideogalleryID')
                ->leftJoin('ut_matches_videos','ut_matches_videos.VideoID=ut_video_to_gallery.VideoID')
                ->leftJoin('ut_matches_videos m1','m1.VideoID=ut_videogallery.PreviewID')
                ->where(['ut_videogallery.Active'=>1])
                //->andWhere(['TournamentID'=>$list['selected']])
                ->groupBy('ut_videogallery.VideogalleryID')
                ->orderBy('ut_videogallery.Date DESC')
                ->limit(self::$countPerPage)
                ->asArray()->all();
        foreach ($gallery as $key => $value) {
            if($value['count']==0) unset($gallery[$key]);
        }
        foreach($gallery as $k=>$v){
            if(isset($v['Data']))
                $v['Data'] = str_replace('http:','https:',$v['Data']);
            if(isset($v['DataP']))
                $v['DataP'] = str_replace('http:','https:',$v['DataP']);
            $gallery[$k]=$v;
        }
        $galleryCount = \common\models\UtVideogallery::find()->select(['ut_videogallery.VideogalleryID',
                'ut_videogallery.Name_rus','ut_videogallery.TournamentID','ut_videogallery.MatchID',
                'ut_videogallery.Date','COUNT(ut_video_to_gallery.VideoToGalleryID) count','ut_matches_videos.Data'])
                ->leftJoin('ut_video_to_gallery','ut_video_to_gallery.VideogalleryID=ut_videogallery.VideogalleryID')
                ->leftJoin('ut_matches_videos','ut_matches_videos.VideoID=ut_video_to_gallery.VideoID')
                ->leftJoin('ut_matches_videos m1','m1.VideoID=ut_videogallery.PreviewID')
                ->where(['ut_videogallery.Active'=>1])
                //->andWhere(['TournamentID'=>$list['selected']])
                ->groupBy('ut_videogallery.VideogalleryID')   
                ->orderBy('ut_videogallery.Date DESC')
                ->asArray()->all();
        foreach ($galleryCount as $key => $value) {
            if($value['count']==0) unset($galleryCount[$key]);
        }
        $pagination = new \yii\data\Pagination(
        [
            'totalCount' => count($galleryCount),
            'pageSize' => self::$countPerPage,

        ]);

Picasso of PHP

By chernomorets, 2017-03-03 11:26:18