Hi I read about in the Documentation that the Controller should be much thinner than the Models. Based on the mass of generated common model files for the DB it fits. But based on the lines of code and the code density the Site-Controller is quite fat.
So the question or discussion is: What is sensible to optimize in the Controller?
The fattest thing is the actionIndex for logged-in users where many information are provided. And where a lot of information comes from different DB tables. For sure the user information itself can be populated in the Models as well as relational data. But now what is with something like this:
In my opinion is a mix of optimisation and personal opinion…
For me model must be data provider and all logic must reside in controller as MVC should be.
This way you find the code where you expect.
Going back to you code your foreach create anyway data so you can move it in a model.
But these data are required just in the index action so rather than moving in the main model and making it fat with a function that aggregate data from different model I would create a new model which handle these ( and may be some other) data.
If you want to create a model that expand the basic one or create a new one is up to you.
This way the controller became slim, the main model do not became fat with a function that is called only in one specific place, the code will result more organized.
that’s my 2 cents.
This is because you use lazy loading for the query.
Everytime I call a method it is a query. I could reduce those by merging the data before and use eager loading. But then I have huge arrays with all information in one variable. And this could be quite difficult in reading the code afterwards or?