CodeIgniter Forums
Customer model design question - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Customer model design question (/showthread.php?tid=12370)



Customer model design question - El Forum - 10-16-2008

[eluser]blasto333[/eluser]
I have a Customer model in a program I am building.

A customer has the following fields in the database table.

first_name, last_name, phone_number, email, street_address, state, zip, comments (and possible more)

In my Model I have a function to save (insert or update) a customer.

Code:
function save_customer($customer_id, $first_name, $last_name, $phone_number, $email, $street_address, $state, $zip, $comments)


As you can tell the function has a LOT of parameters. Is this considered a bad practice? Should I just have one array that contains field=>value references? What do you do?


Customer model design question - El Forum - 10-16-2008

[eluser]OES[/eluser]
For me I pass the post array to the model here is an example.

1. Controler which passes the form data to the model.
Code:
// $data will hold your post array
$data = $this->input->post('input');
if ($this->validation->run() == FALSE):
    // Post your error report
else:
    if ( $this->News_model->insert_news( $this->input->post('input'))):
        // success do whatever
    else:
        // Post error
    endif;
endif;

2. Then in the model I check against any missing data and enter data. Here is a very simple example.
Code:
function insert_news( $data )
{
    $checks = array('title', 'body', 'etc');
        
    foreach($checks as $check):
        if( $check == ''):
            return FALSE;
        endif;
    endforeach;
    
    // Add Date
    $data['date'] = time();
    
    $this->db->set($data);    
    if(!$this->db->insert('todo')):
        return false;                        
    else:
      return true;
    endif;        
}

Hope this helps.