4
In the MVC Standard, the Model is responsible for the business layer, in it will be the business rules and validations. I have a question about the following. A method in the controller that takes any parameter (can be a primitive type or an object). It calls the model to perform an action (such as persisting in the database, changing or deleting).
The first question: should the Model wait for the parameter to be correct? That is, there must be a validation in the controller to know if the parameter came correctly or the parameter must be passed to the Model as it was sent to the controller and the Model must do this validation?
The second question: if the Model validates the parameter how should the return be treated? Let’s say the action is to insert a new entity into the database. And that the default is to return that entity’s ID in case of success. In case of error, which would be the best decision, return 0 as an error, launch an Exception and return 0 or it would be better to refactor these actions so that they return something that can be used both in case of success and in case of return?
I am working on a project made in Silex with Doctrine DBAL, but that was not done by me and I am having these doubts because I see that the application does not follow a correct and standardized flow in these situations. Right here the controller has more responsibility than the Model and this has made me evaluate the refactoring.
Thanks Adriano. But I could edit your answer more clearly. Like, following a little my doubts. I realized that the model should be independent but it was not completely clear the answers to my questions.
– Luciano Lima
Edited ;) Colleague @Papa Charlie’s response can help you more in implementing the solution. hug
– Adriano Godoy
Thank you @Adriano-Godoy. It’s clearer now!
– Luciano Lima