What would be the best practice, within the MVC standard, to process the data?

Asked

Viewed 74 times

4

An example (I usually use Cakephp, but the question would be for any framework and language): to save date type fields in the finger bank I have to transform a format field dd/mm/yyyy in yyyy-mm-dd. To do so, I use a simple function called converteData(data) which is inside the Appcontroller (the mother class of all Cakephp controls). So, the conversion looks like this:

$this->request->data['Cliente']['vinculoDataInicio'] = $this->converteData($this->request->data['Cliente']['vinculoDataInicio']);

I usually do this within the controls. Thus, the data is passed in the right format to the model, which will still validate them before saving them.

My question is: is this the ideal or should I simply pass all the data to the model for it to process, validate and save the data?

  • 1

    The controller’s responsibility is usually to fulfill the requests (requests) of the users, treat a date to me seems a specific task of another class :P. Remember classes are not function repositories.

  • @rray then the correct one would pass everything for the model? I put this function in Appcontroller only to make it available to all other controls, since its use is recurring.

  • @Bacco da view? I disagree. The view has usefulness for the user, and as this system is made for Brazilian users, it must follow the conventions that are used here. I can’t force someone to use the yyyy-mm-dd format. Even if I wanted to, the customer would charge me this.

  • I still don’t agree. I don’t think it’s ideal to use a front-end solution. I think this would be a little more difficult to implement and less consistent, since the user could make page changes.

  • I would do in the same model. There you will probably validate the value, would just do a parse before validation.

  • You are doing well, who is responsible for handling data is precisely the controller, your model has to be responsible only for recording/reading external data. Imagine that you don’t need to process data in the controller and that you pass it all directly to the model......for what would the controller do? you could pass everything directly to the model without intermedios agrees?

  • I removed the comments because I was thinking about leaving the data generically. Anyway worth what I meant about not working with the data in DD/MM/YYYY, but convert as soon as possible (in the appropriate layer, of course). In this case, I don’t know how cake works, but if it was something in ajax, for example, I might not be in charge of DD/MM/YYYY or the transmission, but it really depends on the specific architecture.

  • 1

    @Rafaelacioly There’s a great deal of controversy about this. It will always depend on the specific case, and it may be the case to use a totally separate class (in Cake terminology, a class vendor). Interesting reading: http://blog.joncairns.com/2013/04/fat-model-skinny-controller-is-a-load-of-rubbish/

  • On second thought, I think this is a model-related task. We are talking about pure data that will be saved in the database, and it is the model that should do this.

Show 4 more comments
No answers

Browser other questions tagged

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