public function actionSearch()
 {
     Url::remember();
     //记录当前页,为更新数据后还是跳转到当前页做记录
     $request = Yii::$app->request;
     $query = Yii::$app->session->getFlash('query');
     if ($request->isPost) {
         $type = $request->post('type');
         $content = $request->post('content');
     } else {
         $type = $request->get('type');
         $content = trim($request->get('content'));
     }
     if ($type || !$query) {
         switch ($type) {
             case 'testLibraryId':
                 $query = TestLibrary::find()->where(['testLibraryId' => $content]);
                 break;
             case 'testType':
                 $table_a = TestLibrary::tableName();
                 $table_b = TestType::tableName();
                 $query = TestLibrary::find()->leftJoin($table_b, "{$table_a}.testTypeId={$table_b}.testTypeId")->where(['like', "{$table_b}.name", $content]);
                 break;
             case 'province':
                 $table_a = TestLibrary::tableName();
                 $table_b = Province::tableName();
                 $query = TestLibrary::find()->leftJoin($table_b, "{$table_a}.provinceId={$table_b}.provinceId")->where(['like', "{$table_b}.name", $content]);
                 break;
             case 'majorJob':
                 $table_a = TestLibrary::tableName();
                 $table_b = MajorJob::tableName();
                 $query = TestLibrary::find()->leftJoin($table_b, "{$table_a}.majorJobId={$table_b}.majorJobId")->where(['like', $table_b . ".name", $content]);
                 break;
             case 'testChapter':
                 $table_a = TestLibrary::tableName();
                 $table_b = TestChapter::tableName();
                 $query = TestLibrary::find()->leftJoin($table_b, "{$table_a}.testChapterId={$table_b}.testChapterId")->where(['like', $table_b . ".name", $content]);
                 break;
             case 'question':
             case 'problem':
             case 'options':
                 $query = TestLibrary::find()->where(['like', $type, $content]);
                 break;
             default:
                 $query = TestLibrary::find();
                 break;
         }
     }
     Yii::$app->session->setFlash('query', $query);
     $pagination = new Pagination(['defaultPageSize' => Yii::$app->params['pageSize'], 'totalCount' => $query->count()]);
     $model = $query->offset($pagination->offset)->limit($pagination->limit)->all();
     return $this->render('index', ['models' => $model, 'pages' => $pagination]);
 }
Example #2
0
 /**
  * 根据用户查询一定数量的题目
  * @param $user
  * @param $testTypeId
  * @param $limit
  * @param $offset
  * @return array|\yii\db\ActiveRecord[]
  */
 public static function findByUserAndTestType($user, $testTypeId, $limit, $offset)
 {
     if ($testTypeId == -1) {
         return TestLibrary::find()->where(['provinceId' => $user['provinceId'], 'majorJobId' => $user['majorJobId']])->limit($limit)->offset($offset)->asArray()->all();
     } else {
         return TestLibrary::find()->where(['provinceId' => $user['provinceId'], 'majorJobId' => $user['majorJobId'], 'testTypeId' => $testTypeId])->limit($limit)->offset($offset)->asArray()->all();
     }
 }