本文整理汇总了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]);
}