Answer the question
In order to leave comments, you need to log in
How to search within a group of questions?
Good afternoon, I ran into a problem.
There is a FAQ page, it currently has 6 groups in each group with 6 questions / answers.
I want to do such a thing, usually the search occurs immediately by the fact that it is selected from the database.
I will give a screen
How to implement a search not by the name and description of the group, but to search for questions that are inside the group.
I tried to do this, but so far there are few ideas :(
Make a binding and try to find it. But how to do it more and more competently?
public function actionFaq()
{
$searchQuestion = Yii::$app->request->get('searchQuestion');
$faq = (new Query())
->from(Questions::tableName()." question");
if(!empty($searchQuestion)) {
$faq->leftJoin(AnswerForQuestion::tableName()." searchAnswer")
->andWhere([
'or',
['like','searchAnswer.question',$searchQuestion],
]);
}
return $this->render('faq',[
'faq' => $faq->all()
]);
}
Answer the question
In order to leave comments, you need to log in
Here is the solution, came to this conclusion
public function actionFaq()
{
$searchQuestion = Yii::$app->request->get('searchQuestion');
$faq = (new Query())
->from(Questions::tableName()." quest");
if(!empty($searchQuestion)) {
$faq->leftJoin(AnswerForQuestion::tableName()." ans_for_que",'ans_for_que.id_question = quest.id')
->select('ans_for_que.id_question as id_question, quest.title, quest.decsription')
->groupBy(['id_question','quest.id'])
->andWhere([
'or',
['like','ans_for_que.question',$searchQuestion],
]);
}
return $this->render('faq',[
'faq' => $faq->all()
]);
}
I don't see a difference. The standard situation, there is a table. You are looking for matches in it. What exactly is the problem?
Did what? Doesn't work as expected, are there any errors? Which?
Why join at all? You have a table in which there are questions and answers of all groups, no matter how many there are. The search is done in one line:
$q = 'some query string';
$data = MyModelForQuestion::find()
->orWhere(['like','question',$q])
->orWhere(['like','answer',$q])
->index('group_id')
->all();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question