• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
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).

#2
[eluser]Juan Velandia[/eluser]
1. I would create a function in the model to insert data. And I would call it from the controller

2. I would use Active Record as long as I can.

you could check this: http://henrihnr.wordpress.com/2009/04/26...plication/ and really learn a lot.

#3
[eluser]jprateragg[/eluser]
1. That's what I was planning on doing. I was just unsure about how to pass the data to the model method.

2. Thanks for the link. After reading it over, it appears the author prepares an array at the controller and passes that to the model.

Code:
$person = array('name' => $this->input->post('name'),
    'gender' => $this->input->post('gender'),
    'dob' => date('Y-m-d', strtotime($this->input->post('dob'))));
    $id = $this->personModel->save($person);

Is this the best way to prepare data and send it to the controller? Thanks!

#4
[eluser]Juan Velandia[/eluser]
Well to be honest I'm not an expert in CI to decide the "best way". But I always try to create an array with the data to pass to the model and use the $this->db->insert_batch(); so I think you can go with it ... and you can check:

http://ellislab.com/codeigniter/user-gui...tml#insert

they recomend to use an array for inserting data, so go with it!



Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.