/** * Update blog views counter. * * @param PostBase $model Model */ protected function updateViews($model) { $cookieName = 'page-view-views'; $shouldCount = false; $views = Yii::$app->request->cookies->getValue($cookieName); if ($views !== null) { if (is_array($views)) { if (!in_array($model->id, $views)) { $views[] = $model->id; $shouldCount = true; } } else { $views = [$model->id]; $shouldCount = true; } } else { $views = [$model->id]; $shouldCount = true; } if ($shouldCount === true) { if ($model->updateViews()) { Yii::$app->response->cookies->add(new Cookie(['name' => $cookieName, 'value' => $views, 'expire' => time() + 86400 * 365])); } } }
/** * @return \yii\db\ActiveQuery */ public function getPost() { return $this->hasOne(PagePost::className(), ['id' => 'post_id']); }