Multiple insertions via foreach in MVC

2

I have an application that receives several observations from a view and needs to insert this into a table of observations.

I want to know if in this case, controller is who does foreach and then calls the method in the model to insert one by one or if the model itself is who is responsible for that foreach .

What would be the ideal with MVC? And how do you always think when using foreach in MVC applications, ie which layer is responsible for what?

Example:

  • In a customer database, I can enter several comments;
  • Storing these observations is done in a client_obs table;
  • The process for today's storage is:

    foreach($input['observacoes'] as $observacao){
        insert cliente_obs($observacao) //exemplo
    }
    
  • This process is located in controller

asked by anonymous 04.12.2014 / 15:21

1 answer

1

Your current business requirement

  • In a customer database, I can enter several comments

Note that by describing the text, it says I can insert multiple comments . Because this process is involved in updating an object's state, it will be involved in a transaction. You should do the inserts loop within the same transaction and only commit at the end of the process. So, if you have a problem in the meantime and want to abort the action (you will also want to display an error message for the user [which will originate from an Exception]), none of the comments will be saved in the database.

Being clear, your inserts loop should not be on a Controller.

Learn more about ACID principles

Extra Edition : I'm also putting an explanation of DDD that exactly matches your question:

  DDD is a collection of standards and principles that help you build applications that reflect understanding and satisfaction of your business requirements ...

See this detailed explanation on DDD , TDD and BDD .

    
04.12.2014 / 17:18