Multiple foreach inserts in MVC

Asked

Viewed 106 times

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.

  • 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?

  • I like her. I just woke up with 'this is wrong' in my mind and I wanted to clear the doubt.

  • 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.

  • 1

    @Brunogugusto "Fat model"? Not recommend. In a good design, everyone has to be skinny! D

  • 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.

  • 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.

  • 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.

  • 1

    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.

Show 4 more comments

1 answer

1


Your current business requirement

  • In a customer register, I can insert several observations

Note that from the description of the text, it says I can insert several comments. As this process is involved in updating an object’s state, it will consequently be involved in a transaction. You should loop the Inserts within the same transaction and only at the end of the commit process. So, if you have any problem in the meantime and want to abort the action (you will also want to present an error message to the user [which will be originated from an Exception]), none of the comments will be saved in the database.

Be more clear, your Insert loop should not be in a Controller.

Learn more about ACID principles

extra edition: I am also putting an explanation about DDD that goes exactly against your doubt:

... DDD is a collection of standards and principles that help in your efforts to build applications that reflect an understanding and satisfaction of your business requirements ...

See this detailed explanation about DDD, TDD and BDD.

  • So the controller does nothing? Just take the collection and send it to the model?

  • Very good the ACID

  • 1

    ^ The mission of the high-level controller is to receive the data model and delegate it to a method that does something (usually the business method when it comes to web systems). In your case, your business clearly says it expects a list of comments.

  • 1

    I do not think your question is a case of "mainly based on opinions", quite the contrary, even I have caught myself with this doubt in the past and after researching, it became clear in my head that it is not a "matter of opinion". I voted to reopen :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.