當前位置: 首頁>>代碼示例>>PHP>>正文


PHP Query::sum方法代碼示例

本文整理匯總了PHP中yii\db\Query::sum方法的典型用法代碼示例。如果您正苦於以下問題:PHP Query::sum方法的具體用法?PHP Query::sum怎麽用?PHP Query::sum使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在yii\db\Query的用法示例。


在下文中一共展示了Query::sum方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: actionIndex

 /**
  * Dashboard
  *
  * @return mixed
  */
 public function actionIndex()
 {
     // Unique users today
     $usersQuery = new Query();
     $usersQuery->select(['domain_userid'])->from('{{%event}}')->where('DATE(FROM_UNIXTIME(collector_tstamp)) = CURRENT_DATE')->distinct();
     // Unique users total
     $totalUsersQuery = new Query();
     $totalUsersQuery->select(['users'])->from('{{%stats_performance}}')->where('DATE(day) != CURRENT_DATE');
     // Submissions today
     $submissionsQuery = new Query();
     $submissionsQuery->select(['id'])->from('{{%form_submission}}')->andWhere('DATE(FROM_UNIXTIME(created_at)) = CURRENT_DATE');
     // Submissions total
     $totalSubmissionsQuery = new Query();
     $totalSubmissionsQuery->select(['id'])->from('{{%form_submission}}');
     // If not admin
     if (!empty(Yii::$app->user) && !Yii::$app->user->can("admin")) {
         // Add user filter to queries
         $formIds = Yii::$app->user->getAssignedFormIds();
         $formIds = count($formIds) > 0 ? $formIds : 0;
         // Important restriction
         $usersQuery->andFilterWhere(['app_id' => $formIds]);
         $totalUsersQuery->andFilterWhere(['app_id' => $formIds]);
         $submissionsQuery->andFilterWhere(['form_id' => $formIds]);
         $totalSubmissionsQuery->andFilterWhere(['form_id' => $formIds]);
     }
     // Execute queries
     $users = $usersQuery->count();
     $totalUsers = $totalUsersQuery->sum('users');
     $submissions = $submissionsQuery->count();
     $totalSubmissions = $totalSubmissionsQuery->count();
     // Add today data to total
     $totalUsers = $totalUsers + $users;
     // Users / submissions = Conversion rate
     $submissionRate = 0;
     if ($users > 0 && $submissions > 0) {
         $submissionRate = round($submissions / $users * 100);
     }
     $totalSubmissionRate = 0;
     if ($totalUsers > 0 && $totalSubmissions > 0) {
         $totalSubmissionRate = round($totalSubmissions / $totalUsers * 100);
     }
     // Most viewed forms list by unique users
     $formsByUsersQuery = (new Query())->select(['f.id', 'f.name', 'COUNT(DISTINCT(e.domain_userid)) AS users'])->from('{{%event}} AS e')->innerJoin('{{%form}} AS f', 'e.app_id = f.id')->where(['event' => 'pv'])->andWhere('DATE(FROM_UNIXTIME(collector_tstamp)) = CURRENT_DATE')->groupBy(['id'])->orderBy('users DESC')->limit(Yii::$app->params['ListGroup.listSize']);
     // Forms list by submissions
     $formsBySubmissionsQuery = (new Query())->select(['f.id', 'f.name', 'COUNT(fs.id) AS submissions'])->from('{{%form_submission}} AS fs')->innerJoin('{{%form}} as f', 'fs.form_id = f.id')->where('DATE(FROM_UNIXTIME(fs.created_at)) = CURRENT_DATE')->groupBy(['f.id'])->orderBy('submissions DESC')->limit(Yii::$app->params['ListGroup.listSize']);
     // Last updated forms list
     $lastUpdatedFormsQuery = (new Query())->select(['id', 'name', 'updated_at'])->from('{{%form}} AS f')->where('DATE(FROM_UNIXTIME(updated_at)) = CURRENT_DATE')->orderBy('updated_at DESC')->limit(Yii::$app->params['ListGroup.listSize']);
     // If not admin
     if (!empty(Yii::$app->user) && !Yii::$app->user->can("admin")) {
         // Add user filter to queries
         $formIds = Yii::$app->user->getAssignedFormIds();
         $formIds = count($formIds) > 0 ? $formIds : 0;
         // Important restriction
         $formsByUsersQuery->andFilterWhere(['f.id' => $formIds]);
         $formsBySubmissionsQuery->andFilterWhere(['fs.form_id' => $formIds]);
         $lastUpdatedFormsQuery->andFilterWhere(['f.id' => $formIds]);
     }
     $formsByUsers = $formsByUsersQuery->all();
     $formsBySubmissions = $formsBySubmissionsQuery->all();
     $lastUpdatedForms = $lastUpdatedFormsQuery->all();
     return $this->render('index', ['users' => $users, 'submissions' => $submissions, 'submissionRate' => $submissionRate, 'totalUsers' => $totalUsers, 'totalSubmissions' => $totalSubmissions, 'totalSubmissionRate' => $totalSubmissionRate, 'formsByUsers' => $formsByUsers, 'formsBySubmissions' => $formsBySubmissions, 'lastUpdatedForms' => $lastUpdatedForms]);
 }
開發者ID:ramialcheikh,項目名稱:quickforms,代碼行數:67,代碼來源:DashboardController.php


注:本文中的yii\db\Query::sum方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。