Welcome Guest, Not a member yet? Register   Sign In
How do you save data from your controllers to your models?
#1

[eluser]jprateragg[/eluser]
I'm till new to CodeIgniter and still experimenting. I have a question about how everyone inserts data into the database from the controller. This is my controller for creating a new analysis

Code:
public function create_analysis_2_individual() {
$this->load->model('Analysis_model', 'Analysis');

$data = array();

$member_id = $this->uri->segment(3);

if($this->input->post()) {
  $this->form_validation->set_rules('character_id', 'Character', 'trim|required');
  $this->form_validation->set_rules('sic_id', 'SIC Code', 'trim|required');
  $this->form_validation->set_rules('import_analysis_id', 'Import Analysis', 'trim');
  $this->form_validation->set_rules('include_member_data', 'Income Member Data', 'trim');
  
  
  if($this->form_validation->run() == TRUE) {
   //this is the part I have questions about--inserting data into the db
   $this->Analysis->create_analysis()
   $member_id = $this->db->insert_id();
   redirect('search/create_analysis_1/'. $member_id);
  }
}

$data['member_id'] = $member_id;
$data['member_info'] = $this->Analysis->get_member_info($member_id);
$data['desc_character'] = $this->Analysis->get_desc_character();
$data['desc_sic'] = $this->Analysis->get_desc_sic();
$data['analyses_by_member'] = $this->Analysis->analyses_by_member($member_id);

$this->load->view('forms_header', $this->display_header_data());
$this->load->view('search/create_analysis_2_individual', $data);
$this->load->view('forms_footer', $this->display_header_data());
}

In your models, do you insert via Active Record or a standard SQL statement? Do you pass data to the model as parameters:

Code:
$this->Analysis->create_analysis($member_id, $this->input->post('character_id'), $this->input->post('sic_id'), $this->input->post('import_analysis_id'), $this->input->post('include_member_data'));

Or do you send no parameters and simply call the post array at the model level?
Code:
public function create_analysis() {
$query = $this->db->query("INSERT INTO analysis_history (member_id, create_date, create_user_id, sic_id, character_id, analysis_type_id) VALUES($id, NOW(), ". $this->session->userdata('id') .", ". $this->input->post('sic_id') .", ". $this->input->post('character_id') .", 1)");
}

I'd like to standardize how I insert data, but I don't know if it's possible to always use Active Record (if that is the preferred method).


Messages In This Thread
How do you save data from your controllers to your models? - by El Forum - 11-07-2012, 12:06 PM



Theme © iAndrew 2016 - Forum software by © MyBB