0

I have looked all over the internet for an answer but I can't find it so I will ask here. I have some forms in site and I am not sure how to pass the $_POST to the model I have thought of some options here they are:

Method 1:

Access the $_POST directly from the model the problem I see with this approach is that now my model is tied down to my view which makes my model not reusable.

Method 2:

Create a $data array which contains the post information but the again if I have 10+ fields in my form assigning all this variables is tedious to do and in the model I have to know what I named the key in the controller.

Method 3:

Passing each $_POST key as a parameter to the model function, but this presents a similar problem to Method 2 that if I have 10+ fields in would be a lot of parameters and if I want to add a new parameter later it's a lot of code to rewrite.

I want like to know how other php programmers are doing this.

Anthony Gainor
  • 1,149
  • 2
  • 10
  • 9
  • i don't have too much experience in MVC, in fact im stargin, but As far as I know shouldn't you pass the post information to a controller? and the pass the post to the desired model, your model should be well scripted enough to form the query, or you could make a specific model extending the standard one, even if it's not reusable, sometimes it happens, not the 100% of your code have to be reusable – aleation Jun 25 '13 at 15:39
  • +1 on what @aleation said, controller handles the POST, do whatever is needed (validation, business logic...) and fills the model. Model shouldn't even know POST exists, it's just POPO (plain old php objects) – Florian F. Jun 25 '13 at 15:45
  • I keep the validation in the model. – Anthony Gainor Jun 25 '13 at 16:42

2 Answers2

0

Not sure about other frameworks but in Yii when you generate a form with a model it uses the model name and the attribute for the name of the form elements. For example if you have a model named User, with attributes for username, firstname and lastname. It would create the form like this:

<form>
    <input type='text' name='User[username]' id='User_username'>
    <input type='text' name='User[firstname]' id='User_firstname'>
    <input type='text' name='User[lastname]' id='User_lastname'>
    <input type='submit'>
</form>

This way all the values are stored in an array. Then you can simply say:

//DONE INSIDE CONTROLLER
$user = new User();
$user->attributes = $_POST['User'];
//or if you need to you can store individual values
$user->username = $_POST['User']['username'];

If you don't have you model setup this way you could easily add a function to your model that you could send the $_POST['User'] array to and set the values.

Pitchinnate
  • 7,517
  • 1
  • 20
  • 37
  • -1: your `User` instance is leaking the encapsulation. – tereško Jun 25 '13 at 15:55
  • @tereško All I was saying is this is the way the Yii framework does it. The OP asked how other frameworks handled post values and that is exactly what I was giving him and example. – Pitchinnate Jun 25 '13 at 16:02
  • 1
    Since i know how horrible Yii is, it would seem as a really bad dea to hold it up as an example. – tereško Jun 25 '13 at 16:03
0

The Model is a layer. First, you need to understand what goes on in the model layer, and you can read about that here.

Now we've got that cleared up, let's address your question. You want to pass data from your view to some sort of service / data mapper in your model layer. This is handled the same as every other request.

  • Perform a request to your application
  • Routing handles sending this request to the appropriate controller
  • You may have some sort of mechanism to allow XMLHttpRequests only to certain controllers / methods. If so, somewhere around your routing you should perform your checks for this.
  • Your model layer (maybe a validation service) validates the post data and makes sure it's what you're expecting. I have, in the past, made sure only certain keys / values exist within the post array.
  • Pass this data to a service in the model layer, which then uses it as you see fit.

Basically, treat $_POST requests the same as any other request, just check (using either your own or framework built-in methods) that it's the request type and content you require, then use it as normal.


As for your "a lot of fields", and you're worried about code re-usability / not having to repeat yourself, get the data you require from the client-side using a loop (perhaps via the class using JavaScript). Then, if you add any more data in the future, the loop will automatically pick it up and send it over for you.

Think a little more abstract in your model, i.e. it'll be able to handle the loop's input, and you're golden.

Community
  • 1
  • 1
Jimbo
  • 25,790
  • 15
  • 86
  • 131