I
I
Ivan Lykov2018-05-30 12:18:57
Yii
Ivan Lykov, 2018-05-30 12:18:57

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
5b0e6bf998a1e808664862.png
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

2 answer(s)
I
Ivan Lykov, 2018-05-30
@Jhon_Light

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()
    ]);
  }

M
Maxim Timofeev, 2018-05-30
@webinar

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();

This is of course a primitive solution, the search must take into account typos and break lines into words, and often such accuracy is not required. So this is an example.
and by the way your code:
should give error, missing third parameter

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question