CodeIgniter Forums
Insert auto increment using insert_id - 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: Insert auto increment using insert_id (/showthread.php?tid=53855)



Insert auto increment using insert_id - El Forum - 08-11-2012

[eluser]Ralanyo[/eluser]
I have searched to forums for this and the documentation and can't find an example if anyone can help, i'd appreciate it. Thanks

I just wrote a simple function that inserts into two tables fine but i'm having trouble with the insert_id(). Im trying to get the auto increment primary key of the salesperson table to insert into the salespersonid field of the company table. Here is my code, what am i doing wrong. Sorry for the newbie question

Code:
public function insertMore()
{
    $company = array(
    'companyname' => $this->input->post('company'),
    'salespersonid' => $this->db->insert_id()
       );
      
    $sales = array(
    'name' => $this->input->post('salesrep')
       );
      
       $id = array (
      
       );
    
       $results = array (
       $this->db->insert('company', $company),
       $this->db->insert('salesperson', $sales),
      
       );
      
       return $results;
}



Insert auto increment using insert_id - El Forum - 08-11-2012

[eluser]SkiOne[/eluser]
You have to call it after you do the insert not before


Insert auto increment using insert_id - El Forum - 08-11-2012

[eluser]Ralanyo[/eluser]
Can you show me an example of where to put it. I appreciate it if so. Thanks


Insert auto increment using insert_id - El Forum - 08-11-2012

[eluser]SkiOne[/eluser]
Code:
public function insertMore()
{
    $company['companyname'] = $this->input->post('company')


      
    $sales = array('name' => $this->input->post('salesrep'));
      
       $this->db->insert('salesperson', $sales),
       $company['salespersonid'] = $this->db->insert_id()

       $this->db->insert('company', $company),

}

This is sort of how I would do it, plus the line return $results really isn't doing anything

Edit:

However this is not following the MVC design patter, the queries should exist in the model and you should pass the post vars to that function


Insert auto increment using insert_id - El Forum - 08-11-2012

[eluser]Ralanyo[/eluser]
Great! That worked. I've been trying to figure this out for about 3 hours. Thank you. Here is the code that i used based off your recommendations. Basically the same with some semicolons because i was getting errors.

Code:
$company['companyname'] = $this->input->post('company');    
       $sales = array(
       'name' => $this->input->post('salesrep')
       );
      
          $this->db->insert('salesperson', $sales);
         $company['salespersonid'] = $this->db->insert_id();

          $this->db->insert('company', $company);

In regards to the MVC. Right now I have this code in my model and my controller has the following which passess it to the view. Is this not correct MVC?

Code:
public function insertMore()
    {
    $this->form_validation->set_rules('company', 'company', 'required');
  if ($this->form_validation->run() === FALSE)
{
  $this->load->view('companymore');
  
}
else
{
  $this->load->model('Company_model');
  
  $this->Company_model->insertMore();
  $this->load->view('companysuccess');
}




Insert auto increment using insert_id - El Forum - 08-11-2012

[eluser]SkiOne[/eluser]
You might get some that would disagree, but I think you should get the post vars in the controller


Insert auto increment using insert_id - El Forum - 08-11-2012

[eluser]Ralanyo[/eluser]
hmm that makes sense because model is for data objects from what i read. I am new to CI and learning MVC. My brain is also pretty fried right now after working on this app all day.

This is probably a really stupid question, but like i said my brain is not working correctly right now. How do you get the post data in the controller? Do you store it in an array and use the variable in the model?

I read a ci book that says fat model, slim controller. not really sure what that means


Insert auto increment using insert_id - El Forum - 08-11-2012

[eluser]SkiOne[/eluser]
You pass it like a regular old method
Controller:
function controllerFunction()
{
$var = $this->input->post('var');
$this->some_model->function($var);
}

Model:
function function($var)
{
echo $var;
}

Have you watched this video:
http://codeigniter.com/tutorials/watch/intro/


Insert auto increment using insert_id - El Forum - 08-11-2012

[eluser]Ralanyo[/eluser]
Thanks that totally makes sense. Sometime you just need someone to give you a clue and then it just clicks.

I watched the videos a few months ago, but I didn't dive right into CI then. I will check them out again. Thanks for all your help.