I
I
Ilyas Galiev2015-04-03 07:41:13
Yii
Ilyas Galiev, 2015-04-03 07:41:13

Is it correct to store additional methods in the model?

There are models and they contain 20 additional static methods for obtaining different data (sorted differently or separate data used in several places).
Is it right? Or is it better to somehow arrange it in the form of a component, behavior or type of such?
And would it be better to make them not static, but regular methods?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Dozhdikov, 2015-04-03
@MrGaliev

To me, this is not correct.
To get data from one data model in different sorted ways, but to do it through different methods, this is a big overkill of the code. You have CDbCriteria->order, for example, and the sort order is also not a problem to transfer to the model.
And getting different data, if different fields from the same model are meant, is also unnecessary coding, in my opinion. You can always give a permitted set of fields.
If there are many such intersections, then even for readability it is easier to combine these methods, at least to do a banal search(). To select in a component or behavior, probably not. Although all this can be taken out in scopes (), where you can describe both sorting and fields in select.
And for me it’s better to make them not static, but it all depends on the context of the model and its methods.

R
Roman Frank, 2015-04-03
@Akellacom

I think that this is correct. After all, this is what the model is for, to work with the database. The main thing is that you do not duplicate standard methods :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question