Answer the question
In order to leave comments, you need to log in
How to implement the choice of model for search, taking into account the selected value?
In general, there is a form like this.
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
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 questionAsk a Question
731 491 924 answers to any question