I
I
Igor Vasiliev2018-04-12 11:43:08
Yii
Igor Vasiliev, 2018-04-12 11:43:08

How to search across multiple tables in a database in Yii2?

Hello.
--
I wanted to clarify how it would be more correct and safer, with joins or what other DAO queries in Yii2 to search for words or sentences in several tables?
I'm going to use Html::beginForm to not be bound to a specific action, but to pull a request from the database if a request has been made.
That is, there will be some code:

<?= Html::input('text', 'search', $search->name, ['class' => $class]) ?>

Which will be processed by the controller.
For example, there are three tables with which it will work.
"test1" , "test2" , "test3"
...->where(['like', 'search', $search->name])->all();

That is, name="search" that does not exist in any model. It will search through the get-request for matches in the above tables. I understand, the topic is hackneyed, but I want to understand the principles. How to escape table names - I know.
And I don't know how to make such requests correctly. Literacy refers to the load on the server with a large and bulk query, when the search is carried out in several tables, regardless of what the column names are.
I need this so that I myself can learn how to make a sitemap if there was no specific request, or display as a search engine what I indicated in the search field.
To make it more clear, I will use ajax and javascript function, which without clicking on the button will look for the entered data. I did this trick with one table, and I got an error that I made a lot of queries. In general, we need the advice of professionals who understand this more than passing critics.
How can you? And how is it necessary? What is the best way to do this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2018-04-12
@webinar

Think first about how you plan to display the results. If these are three tables, then there are different entities and making one list is not correct. Most likely these are three blocks with results, then these are 3 queries to the database and three separate arrays of objects.
I don't see the logic here, please explain.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question