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]); }
/** * 根据用户查询所有收藏以及与其相关的testLibrary * @param $userId * @return mixed * @throws \Exception */ public static function findAllByUserWithTestLibrary($userId) { $table_a = Collection::tableName(); $table_b = TestLibrary::tableName(); return (new Query())->from([$table_a, $table_b])->where(["{$table_a}.userId" => $userId])->andWhere("{$table_b}.testLibraryId = {$table_a}.testLibraryId")->orderBy(["{$table_a}.createDate" => SORT_DESC])->all(); }
/** * 查询用户当前做到哪种题型的第几题 * @param $user * @param $testTypeId * @return mixed */ public static function findCurrentNumber($user, $testTypeId) { $current = CurrentTestLibrary::findByUserAndTestType($user['userId'], $testTypeId); if (!$current) { return 0; } $table = TestLibrary::tableName(); if ($testTypeId == -1) { $subQuery = (new Query())->select('testLibraryId')->from($table)->where(['provinceId' => $user['provinceId'], 'majorJobId' => $user['majorJobId']]); } else { $subQuery = (new Query())->select('testLibraryId')->from($table)->where(['provinceId' => $user['provinceId'], 'majorJobId' => $user['majorJobId'], 'testTypeId' => $testTypeId]); } $query = (new Query())->select('count(*)')->from([$subQuery])->where(['<=', 'testLibraryId', $current['testLibraryId']]); $result = $query->one(); return $result['count(*)']; }