Example #1
0
 public function actionSearch()
 {
     $searchModel = new Search();
     $keyword = Yii::$app->request->get('keyword');
     if (empty($keyword)) {
         $this->goHome();
     }
     // 记录log
     $model = new SearchLog();
     $model->setAttributes(['user_id' => Yii::$app->user->isGuest ? '' : Yii::$app->user->identity->getId(), 'keyword' => $keyword, 'created_at' => time()]);
     $model->save();
     $dataProvider = $searchModel->search($keyword);
     return $this->render('search', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider]);
 }
Example #2
0
 /**
  * Finds the SearchLog model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return SearchLog the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = SearchLog::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Example #3
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = SearchLog::find();
     $query->orderBy(['created_at' => SORT_DESC]);
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     if ($this->load($params) && !$this->validate()) {
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'user_id' => $this->user_id, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]);
     $query->andFilterWhere(['like', 'session_id', $this->session_id])->andFilterWhere(['like', 'keyword', $this->keyword])->andFilterWhere(['like', 'ip', $this->ip]);
     return $dataProvider;
 }
Example #4
0
 public function actionSearch($keyword = null, $type = self::PRODUCT_SORT_CREATED_AT)
 {
     if ($type == self::PRODUCT_SORT_CREATED_AT) {
         $type = 'created_at';
     } elseif ($type == self::PRODUCT_SORT_SALES) {
         $type = 'sales';
     } else {
         throw new BadRequestHttpException('Type is not supported.');
     }
     if (trim($keyword)) {
         $keyword = trim($keyword);
         $searchLog = new SearchLog(['session_id' => Yii::$app->session->id, 'user_id' => Yii::$app->user->id, 'keyword' => $keyword, 'ip' => Yii::$app->request->userIP]);
         $searchLog->save();
         $query = Product::find()->where('name like "%' . $keyword . '%"');
     } else {
         $query = Product::find();
     }
     $query->orderBy(['sales' => SORT_DESC]);
     $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['defaultPageSize' => Yii::$app->params['defaultPageSizeProduct']], 'sort' => ['defaultOrder' => ['created_at' => SORT_DESC]]]);
     return $this->render('search', ['products' => $dataProvider->getModels(), 'pagination' => $dataProvider->pagination]);
 }
Example #5
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = SearchLog::find()->joinWith('user');
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $dataProvider->sort->attributes['username'] = ['asc' => ['username' => SORT_ASC], 'desc' => ['username' => SORT_DESC]];
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'user_id' => $this->user_id, 'created_at' => $this->created_at]);
     $query->andFilterWhere(['like', 'keyword', $this->keyword])->andFilterWhere(['like', 'username', $this->username]);
     return $dataProvider;
 }