Welcome Guest, Not a member yet? Register   Sign In
Use of $_POST in controller vs model
#1

[eluser]VirtualDevel[/eluser]
Hi, first off, thanks for looking at this. Okay so my confusion is the use of $_POST or any other such super within a controller vs model.
Where is the 'right' place for it. For example, in the user guide under the model section there is this piece of code:

Code:
function insert_entry()
    {
        $this->title   = $this->input->post('title'); // please read the below note
        $this->content = $this->input->post('content');
        $this->date    = time();

        $this->db->insert('entries', $this);
    }

But in other documentation I have seen it done other ways, for example:

Code:
// controller
    $data['title']      = $this->input->post('title')
    $data['content'] = $this->input->post('content')    
    $data['date']      = time();

    $this->model->insert_entry($data)
    

    // model
    function insert_entry( $data = array() )
    {
        $this->db->insert('entries', data);
    }
#2

[eluser]bigtony[/eluser]
Personally I use the controller to set the array which is passed to the model (as per your second example). The advantage of this is that you may sometimes want to insert/update your database from data that doesn't come from a form, e.g. you may have a function that creates some test records automatically.
#3

[eluser]GlennJ[/eluser]
Ideally keep the post data out of your models, as shown by use of an array. But I have to admit, I have used post variables in models before. Normally when I have LOADS of fields to store and need to get it done quickly.

If it turns out later that you need to reuse the function, then replace the naughty post variables with a nice array and update the old code.




Theme © iAndrew 2016 - Forum software by © MyBB