Welcome Guest, Not a member yet? Register   Sign In
Validation -> Model
#1

[eluser]JanDoToDo[/eluser]
Hey guys,

I have a form which has validation rules setup. Once the rules pass, I want to load a model and then add data to a database. My question is - does the model get the input/post data or do i have to pass the data as an array when i call the function in the model?
Cheers
#2

[eluser]Jan_1[/eluser]
If your post-data needs no service you can do it very simple in your model/controller:
Code:
$this->db->insert('db_table', $_POST);
Is that what your asking for?
#3

[eluser]JanDoToDo[/eluser]
I was wondering if the model gets the post data, or whether i have to assign the post data to a variable in the controller and then pass that to the model. i.e.

Can i do this:
(pseudo)
class model

funtion add
$name = this> input> post(name)
this >db> insert($name)


// ALTERNATIVELY

or do i need to do this
(now in the controller)
load> model >(addmodel)

$name = this> input> post(name)
this> addmodel> add_entry($name)
#4

[eluser]Jan_1[/eluser]
there is no necessity. if it's short and simple you can do it in the controller. it's the idea, that if you have it in the model, you can use it in some other functions too and so you might spare time and work.

additionally see the CI-offical tutorial 'blog in 20 minutes':
contoller blog.php:
Code:
function comment_insert()
{
$this->db->insert('comments', $_POST);
redirect('blog/comments/'.$_POST['entry_id']);
}

short and simple - CI
#5

[eluser]Colin Williams[/eluser]
I personally don't like the model accessing $_POST data because that, in a way, couples the model too tightly with the view (what if the data is retrieved by means other than a posted form, like CSV imports, etc). Typically all of my insert-type model functions accept an object to save (or array) which it filters through to remove unneeded properties before doing the SQL. Then a controller can just pass it $_POST, as in the examples above.

Code:
function update()
{
   if ($this->blog_model->save($_POST)
   {
      // Update worked
   }
}




Theme © iAndrew 2016 - Forum software by © MyBB