Welcome Guest, Not a member yet? Register   Sign In
Passing Data To Model
#1

[eluser]wakextreme[/eluser]
How do you normally pass posted data to your models.

* Pass entire $_POST array into the model.
* Access post data via $this->input->post('foo'); from inside the model.
* Read post data from $_POST or $this->input->post into vars inside the conroller and pass the vars into the model

I normally process the posted data in controller and pass it in so that my model methods are not dependent upon posted data, but i'm just curious how others usually handle it.

Thanks
#2

[eluser]tonanbarbarian[/eluser]
I always pass all necessary outside data to the model.
I treat the model as a closed box if possible so that it know nothing of the outside world except what is given to it
#3

[eluser]RobbieL[/eluser]
Usually do your third option. Catch the POST data neede into vars in the controller, and then pass them to the model
#4

[eluser]stevepaperjam[/eluser]
I used to access the $_POST from the model, but that wasn't quite flexible enough so now I pass the $_POST into the model, so number one for me.
#5

[eluser]Don Pinkster[/eluser]
I never give the _POST to the model, only the values I need. This way I can make sure the Model knows nothing of the application or depends on the variables set by PHP.
#6

[eluser]onisemus[/eluser]
I agree with a couple of other posts here. The model should act as a black box. It's your "Data Access Layer", and the only thing it should be about it accessing your data. That means keeping things such as reading post values away from it. Get the post values in the controller. Then pass those values to the model.

The advantage of this is that it lets your different layers (Model, View, and Controller) be loosely coupled. For example, if your view changes (say a input field name), you don't necessarily have to go update the model because its just passing a value. If your model changes (and database column name), your view doesn't necessarily have to change, because it doesn't care about the database field names.

One other alternative, if you want to keep your controller code clean is to create a helper that the controller invokes, which passes all the values to the model for you.

Hope that helps!
#7

[eluser]maadmac[/eluser]
[quote author="wakextreme" date="1196407577"]
* Read post data from $_POST or $this->input->post into vars inside the conroller and pass the vars into the model
[/quote]

This is preferred.




Theme © iAndrew 2016 - Forum software by © MyBB