Welcome Guest, Not a member yet? Register   Sign In
Using $_POST at the model
#11

[eluser]basementDUDE[/eluser]
I usually use input class to process user input.
However, that's one thing the input class cannot do.

Code:
//check whether user has submit the form or not
if(isset($_POST['submit']))
{
//do something
}
#12

[eluser]davidbehler[/eluser]
Sure it can
Code:
if($this->input->post('submit'))
{
//do something
}
#13

[eluser]bigtony[/eluser]
[quote author="adijux" date="1250119926"]Well, CodeIgniter User Guide suggests taking $_POST in the model (at lest the example suggests that: http://ellislab.com/codeigniter/user-gui....html#what).[/quote]
I think this is more a case of the user guide trying to keep the example as simple as possible, and shouldn't be interpreted as the recommended way.

IMO, you should use the $this->input fields rather than $_POST for the reasons already stated in this thread, but, getting back to the question in the OP, NOT use either directly in the model if you want maximum flexibility (i.e to maintain the principle of 'loose coupling').

To give an example, in one application I have a function in a model that will add details about a building, but in some cases this data comes from a form and in other cases it can be generated programatically. Had I used $this->input or $_POST it would not have been possible to call it with the programmatically generated data without duplicating the function.

It's only a small amount of extra work to pass post data into the model rather than use it directly, but worth it in the long run.
#14

[eluser]basementDUDE[/eluser]
[quote author="waldmeister" date="1250251370"]Sure it can
Code:
if($this->input->post('submit'))
{
//do something
}
[/quote]
ouch! thank for point it out.
I was doing something like this.

Code:
if(isset($this->input->post('submit')))

and get this error:
Fatal error: Can't use method return value in write context

ha ha
#15

[eluser]Edmundas KondraĊĦovas[/eluser]
[quote author="bigtony" date="1250254712"]To give an example, in one application I have a function in a model that will add details about a building, but in some cases this data comes from a form and in other cases it can be generated programatically. Had I used $this->input or $_POST it would not have been possible to call it with the programmatically generated data without duplicating the function.

It's only a small amount of extra work to pass post data into the model rather than use it directly, but worth it in the long run.[/quote]

It depends on what you're trying to accomplish. If you want your model to not only gather data from a form, but from elsewhere as well, then it's definitely not good practise to process input values from the model. On the other hand, if you know you're going to use the model just for the form, then it's not all that bad fetching input values from the model.

Maybe it's not the smartest idea, but I use both variants depending on the situation.
#16

[eluser]bigtony[/eluser]
[quote author="adijux" date="1250256095"]On the other hand, if you know you're going to use the model just for the form, then it's not all that bad fetching input values from the model.[/quote]
Agree it's not the worse thing in the world, and in some cases can seem like the most pragmatic solution. But for enterprise level apps, it's good to build in maintainability & flexibility from the start, as well as using a consistent approach across all models, etc. And as already stated, it's not that much extra effort.




Theme © iAndrew 2016 - Forum software by © MyBB