2
I have an application that receives several observations from a view and need to insert this into a table of observations.
I want to know if in this case, controller is who makes the foreach
and then calls the method in the model to insert one by one or if the model itself is who has to be responsible for that foreach
.
What would be the ideal with MVC? And how to always think when using a foreach
in MVC applications, i.e., which layer is responsible for what?
Example:
- In a customer register, I can insert several observations;
- The storage of these observations is done in a table cliente_obs;
The process for storage today is:
foreach($input['observacoes'] as $observacao){ insert cliente_obs($observacao) //exemplo }
This process is located in the controller
OK @Caffé. Updating the question.
– Rafael Soufraz
The controler is responsible for receiving user inputs and integrating them into the model. There is nothing to undo the way you did. There’s something you don’t like about this solution?
– Caffé
I like her. I just woke up with 'this is wrong' in my mind and I wanted to clear the doubt.
– Rafael Soufraz
I believe Iss varies from programmer to programmer. Some prefer the model of the Slim Controller Fat Model where the Controller does little (relatively speaking) while others prefer the opposite, with a slender and objective Model.
– Bruno Augusto
@Brunogugusto "Fat model"? Not recommend. In a good design, everyone has to be skinny! D
– Caffé
Too bad the scope of the site is quite restricted. This would be a good topic to have around here. I’m not exactly an expert on ORM but what I have as Models has only properties that reflect the database columns. Table Manager makes CRUD and Entity Repositories, since recently, take care of specific Selects.
– Bruno Augusto
Yes @Brunoaugusto. I also use Positorios but all my relationships are in models. The Repositorios as well as their own took care only of persistence to base. But I have seen that every rule of business that the GUI has no interference with, who needs to be responsible for it is the model.
– Rafael Soufraz
Example.. If the entire GUI of your system displays the date in d/m/Y, the model has to resolve this before sending it to the controller. But if at some point the GUI only needs to see the week of that date, then the view has to trigger a helper to request the week based on that date in d/m/Y.
– Rafael Soufraz
Well I created my "ORM" (and put quotes on it) the way I know. It wasn’t bad, but the models don’t automatically relate.
– Bruno Augusto