P
P
Pavel Khorikov2015-05-18 10:21:43
Yii
Pavel Khorikov, 2015-05-18 10:21:43

How to implement the choice of model for search, taking into account the selected value?

In general, there is a form like this.
9c504458ed2d4d6eb97cfaeb0504aa7f.png
Depending on the selected value, it is necessary to implement a search in the table of authors (if the appropriate option is selected), or in the table of publications.
How to organize the selection of the desired model for the search? It is known that the first argument in $form->field($model, 'attribute') must be the name of the model in which the search() method will be implemented; How to organize switching between models in my case? Or is it more correct to create a separate model, for example, searchModel and broadcast some kind of handler there? It would be more logical, in my opinion, to create a Search model and implement a search method there so as not to write the same method in two different models, but then another question arises - when generating a model, you must specify a table (one for which you need implement the search method), but in my case there are two of them and they (both models) already exist. Can you please tell me some best practice? Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Tupikov, 2015-05-18
@Horik_off

I would create, as you suggest, a separate Search class inherited from the Model class and implement the search logic in it.
Alternatively, you can move the logic to Trait and connect it in the AuthorSearch and ArticleSearch classes, which are inherited from the corresponding AR models

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question