Welcome Guest, Not a member yet? Register   Sign In
Problem sending post data to model for insert/update
#1

[eluser]Nicholai[/eluser]
I've searched the forums for this and read the user guide, but can't find any examples that are basic enough for me to follow without getting lost. I am new to the MVC concept and am trying to learn it one piece at a time. My goal is to:
1. display a form in a view containing a hidden ID field and some text boxes
2. pre-populate the form with data from a table
3. allow the user to edit the text, and when clicking submit in the form, send the post data to the model to be inserted/updated.

Items 1 and 2 are working and I understand those concepts, but 3 is where I can't figure out how to do it properly. Here is a simplified version of my view:

Code:
<?php foreach($this->Admin_model->get_task_types() as $row): ?>
<?php echo form_open('tasktypes/insert'); ?>
<input type="hidden" name="ID" value="<?php echo $row->ID; ?>"/>
<input type="text" name="ttName" value="<?php echo $row->ttName; ?>"/>
<input type="submit" value="Update" />
</form>
<?php endforeach; ?>

Here is the relevant piece of my model, called Admin_model:
Code:
function save_task_types() {

        $db_array = array("ID" => $this->ID,
        "ttName" => $this->ttName);

        if ($this->ID) {
            $this->db->where("ID", $this->ID);
            $this->db->update("tasktypes", $db_array);
        }

        // If ID was not set in the controller, then we will insert a new record.
        else {
            $this->db->insert("tasktypes", $db_array);
        }
    }

Getting the data from my view to my controller is where I am lost. Based on examples I've seen in the forums, this is what I am trying in the controller:

Code:
function insert()
    {
    $this->load->model('Admin_model');

    $new_array = array(
        $ID => $this->$input->post('ID'),
        $ttName => $this->$input->post('ttName'));
            
        $this->Admin_model->save_task_types($new_array);
        
        redirect('tasktypes/display');
    }

Once I understand how this works I'll move on to learning how to validate, but I feel like I'm missing a core concept here. Any advice would be great.
#2

[eluser]bretticus[/eluser]
There is so much opinion concerning the MVC paradigm, that I feel you're free to "wiggle" a little bit concerning the boundaries of each. However, the general idea is that you separate core logic from your layout. For example, you might want to call your model in your controller and then send it to your view. That way, you'll have very little programming in your view and more flexibility in layout (that's the theory and it makes the most sense where you have programmers and designers doing two different jobs.)

Another thing I would recommend is that your insert action in your controller loads the form by default (see the form validation examples.) The advantage to this is that if there are validation errors, they can be easily sent back to the form. Only on a successful submission would I do a redirect (note: you can even have the insert action load a success view, but the redirect is quick way to limit the possibility of double posting.)

I still think the tutorial videos on this website do a great job of showing the "CI way." If you haven't watched them for awhile, I recommend doing so again.
#3

[eluser]Nicholai[/eluser]
Thanks bretticus. I do understand the idea of separating data manipulation (model), logic (controller), and presentation (view) and can insert data if I follow the example of the "blog in 20 minutes" video. However, the video does a $this->db->insert('table',$_POST) and bypasses models altogether. It doesn't show how to send anything to a model and I haven't found one in the user guide either.

When trying to use the code I wrote, I receive a "fatal error: Cannot access empty property" in my controller. Is there a basic, newbie example of a controller that gets individual pieces of post data from a form submit and sends it to a model function to be processed?
#4

[eluser]Colin Williams[/eluser]
"Getting individual pieces of post data from a form submit" is probably the wrong way to go about it. That puts too much weight on the controller to understand the data. You should be able to toss the whole $_POST array at the model, and the model should weed out any unnecessary properties. As a very simple example:

Controller
Code:
function submit()
{
  $this->some_model->create(xss_clean($_POST)); // Clean here or do it globally.
}

Model
Code:
function create($obj)
{
   $new_data = array(
      'title' => $obj['title'],
      'body' => $obj['body']
   );
   return $this->db->insert('table', $new_data);
}

There are more elegant ways of doing this, but that's the basic idea.
#5

[eluser]Nicholai[/eluser]
Colin, thank you so much. That was exactly what I needed to get off the ground!
#6

[eluser]Colin Williams[/eluser]
Wow! Glad it helped, even with it being so brief. I have a good tut on models appearing either on my site or nettuts soon.
#7

[eluser]medvind[/eluser]
[quote author="Colin Williams" date="1240129535"]"Getting individual pieces of post data from a form submit" is probably the wrong way to go about it. That puts too much weight on the controller to understand the data. You should be able to toss the whole $_POST array at the model, and the model should weed out any unnecessary properties.[/quote]

I have a model named Db_table and for each database table, I have models that extend it. As I see it, the processing of post data could be done in either of these two parts:

1. The controller, before passing it to the model
2. The model that corresponds to the appropriate database table, before passing it to the Db_table method for inserting data.

Bonus alternative: 3. I could choose not to process the data, and let Db_table try to tuck it in and see if it fits (i.e. if the $_POST array contains the appropriate number of fields and values)

So 2 is the way to go? Just to make it very clear for us MVC newbies Smile




Theme © iAndrew 2016 - Forum software by © MyBB