Created
April 7, 2016 04:16
-
-
Save AlexMcowkin/5b7f174c5aeacc3e8264e57f369d585c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function actionSearch() | |
{ | |
// ---------SphinxSearch---------- | |
if($_SERVER['REQUEST_METHOD'] == 'GET') | |
{ | |
$searchText = HtmlPurifier::process(Yii::$app->request->get('search')); | |
$searchText = trim($searchText); | |
$sphinxQuery = new Query(); | |
$sphinxResult = $sphinxQuery->from('post_index')->match($searchText)->all(); | |
$totalSphinx = count($sphinxResult); | |
if($totalSphinx > 0) | |
{ | |
foreach ($sphinxResult as $sphinxRow) | |
{ | |
$ids[] = $sphinxRow['id']; | |
} | |
arsort($ids); | |
$searchResult = PostModel::find()->where(['id'=>$ids, 'status'=>1])->orderBy(['date_added' => SORT_DESC]); | |
$countQuery = clone $searchResult; | |
$pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize'=>$this->postPerPage]); | |
$models = $searchResult->offset($pages->offset)->limit($pages->limit)->all(); | |
return $this->render('search_page', ['searchText' => $searchText, 'searchResult' => $models, 'pages' => $pages, 'totalPosts' => $totalSphinx]); | |
} | |
else { return $this->render('search_page', ['searchText' => $searchText, 'totalPosts' => 0]); } | |
} | |
else { return $this->redirect(URL::home()); } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment