function run() { $cacheId = $this->timec . 'post'; $baseQuery = Post::find()->select(['id', 'title'])->orderBy('created_at'); if (($modelsPrev = Yii::$app->cache->get($cacheId . 'prev')) === false) { $modelsPrev = $baseQuery->where(['>', 'created_at', $this->timec])->one(); Yii::$app->cache->set($cacheId . 'prev', $modelsPrev, 3600); } if (($modelsNext = Yii::$app->cache->get($cacheId . 'next')) === false) { $modelsNext = $baseQuery->where(['<', 'created_at', $this->timec])->orderBy('created_at DESC')->one(); Yii::$app->cache->set($cacheId . 'next', $modelsNext, 3600); } return $this->render('PaginationPrevNext', compact('modelsPrev', 'modelsNext')); }
/** * Finds the Post model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Post the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Post::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
function run() { $modelsPrev = Post::find()->where(['>', 'created_at', $this->timec])->select(['id', 'title'])->limit(1)->orderBy('created_at')->all(); $modelsNext = Post::find()->where(['<', 'created_at', $this->timec])->select(['id', 'title'])->limit(1)->orderBy('created_at DESC')->all(); return $this->render('PaginationPrevNext', compact('modelsPrev', 'modelsNext')); }