예제 #1
0
 public function actionSearch()
 {
     $request = Yii::$app->request;
     $type = $request->get("type");
     $query = Yii::$app->session->getFlash('query');
     if (!$type) {
         $type = $request->post("type");
         $content = $request->post("content");
     } else {
         $content = $request->get("content");
     }
     if ($type || !$query) {
         switch ($type) {
             case 'userId':
                 $query = Service::find()->where(['userId' => $content]);
                 break;
             case 'date':
                 $query = Service::find()->where(['between', 'createDate', date('Y-m-d H:i:s', strtotime($content)), date('Y-m-d H:i:s')]);
                 break;
             case 'state':
                 if ($content == 'noReply') {
                     $query = Service::find()->where(['state' => Service::STATE_UNREPLY]);
                 } elseif ($content == 'replied') {
                     $query = Service::find()->where(['state' => Service::STATE_REPLIED]);
                 } elseif ($content == 'publish') {
                     $query = Service::find()->where(['state' => Service::STATE_PUBLISH]);
                 } else {
                     $query = Service::find();
                 }
                 break;
             case 'nickname':
                 $table_a = Service::tableName();
                 $table_b = Users::tableName();
                 $query = Service::find()->leftJoin($table_b, "{$table_a}.userId={$table_b}.userId")->where(['like', "{$table_b}.nickname", $content]);
                 break;
             case 'content':
                 $query = Service::find()->where(['like', $type, $content]);
                 break;
             default:
                 $query = Service::find();
                 break;
         }
     }
     $pagination = new Pagination(['defaultPageSize' => Yii::$app->params['pageSize'], 'totalCount' => $query->count()]);
     $models = $query->offset($pagination->offset)->limit($pagination->limit)->orderBy(['createDate' => SORT_DESC])->all();
     return $this->render('index', ['models' => $models, 'pages' => $pagination]);
 }