Welcome Guest, Not a member yet? Register   Sign In
Forms and Databases (Best Practice)
#1

[eluser]thors1982[/eluser]
So, I am fairly new to Codeigniter and am wondering what the best practice is.

I see many examples of where they get the post... pass in the post to the model and store all the data. But I have a few extra fields in my form that are not supposed to be in the database. Such as how the information should be saved.
Examples:
Draft, Publish, Revert

Basically what I have done is instead of sending all the post information to the model and storing it in the DB I call a function(in the controller) which just sets only the vars I need in a new object. Then I pass the new object to the model and store everything.

Example:
Code:
$newpage->id = $this->input->post('id');
$newpage->page_name = $this->input->post('page_name');
$newpage->navigation_id = $this->input->post('navigation_id');
$newpage->titlebar = $this->input->post('titlebar');
$newpage->heading = $this->input->post('heading');
$newpage->subhead = $this->input->post('subhead');
$newpage->content = $this->input->post('content');
$newpage->meta_keywords = $this->input->post('meta_keywords');
$newpage->meta_desc = $this->input->post('meta_desc');

Is this a normal/good practice or should I be doing something else?

The reason I ask is because if i add a new field to the form it has to be updated in several places.

Thanks for the help in advance
#2

[eluser]Jay Logan[/eluser]
Why not put the info in the model and just call to the model when it's time to post to DB?

Code:
$newpage->id = $this->input->post('id');
$newpage->page_name = $this->input->post('page_name');
$newpage->navigation_id = $this->input->post('navigation_id');
$newpage->titlebar = $this->input->post('titlebar');
$newpage->heading = $this->input->post('heading');
$newpage->subhead = $this->input->post('subhead');
$newpage->content = $this->input->post('content');
$newpage->meta_keywords = $this->input->post('meta_keywords');
$newpage->meta_desc = $this->input->post('meta_desc');
$this->db->insert('table_name', $newpage);
#3

[eluser]Vicente Russo[/eluser]
Hi,

Thats what I do:
Code:
// Form sent        
$excluded = array('submit','i_agree_radio_button'); // Fields excluded from insert sql
$fieldnames = array( // Just example:
    'field_name_on_form' => 'field_name_on_table',
    'name' => 'name',
    'address_txt' => 'address'
);

$post_temp = array();
foreach($_POST as $index => $value) {
    if(!in_array($index,$excluded)) {
        $post_temp[$fieldnames[$index]] = $this->input->post($index);                    
    }
}

$this->db->insert('table_name', $post_temp);

But its possible to fill the $fieldnames array automatically, and if you put this on a model, ou just have to mess in one place
#4

[eluser]thors1982[/eluser]
Thanks Vicente Russo

I ended up taking your suggestion its basically the same thing just much more elegant and clean. :-) I don't know if I was looking for that or something more automatic. But this definitely feels/looks much better.

And I did end up moving it all to my model as well. So thank you both for that.
#5

[eluser]jpeffer[/eluser]
I generally use the field names defined within my form validator definition to extract only those fields from the POST. Something like the following:
Code:
// Remove keys from the registration_data array that were not validated
$registration_data = array_intersect_key($registration_data, $this->CI->form_validation->_field_data);

$registration_data is set as the method parameter in my authentication lib. All I then have to do from the controller is simply pass the $_POST array.... done




Theme © iAndrew 2016 - Forum software by © MyBB