Welcome Guest, Not a member yet? Register   Sign In
How do I return the insert_id value from a database model to use in a query?
#1

[eluser]dallen33[/eluser]
Well, I do know how to return it, but I'm clearly doing something wrong. So in my database model:
Code:
$this->db->insert('online_booking', $form_data);
$booking_id = $this->db->insert_id();
return $booking_id;
Simple enough. In my controller, though, I want to do this:
Code:
if ($this->form_model->online_booking($this->input->post())):
$this->data['order'] = $this->db->get_where('online_booking', array('id' => $id))->row();
Instead of having $id in my query, I want to get the returned value of my database model. I've tried doing this:
Code:
$this->data['order'] = $this->db->get_where('online_booking', array('id' => $this->form_model->online_booking($this->input->post())))->row();
But I assume that means it's actually doing the query twice. Which I don't want.

What's the proper way to do this?
#2

[eluser]dallen33[/eluser]
I wish I could delete this! Did this instead:
Code:
$form_db = $this->form_model->online_booking($this->input->post());
if ($form_db)
Duh.
#3

[eluser]InsiteFX[/eluser]
Code:
$id = $this->db->insert_id();
#4

[eluser]dallen33[/eluser]
[quote author="InsiteFX" date="1332526108"]
Code:
$id = $this->db->insert_id();
[/quote]
Would that still work in the controller after the db stuff is being done in a model?
#5

[eluser]xtremer360[/eluser]
No what you would need to do is after you run your query in your model then assign an id variable to the insert id as per the code above and then return it from your model function. So you'd end up with:

Code:
function queryFunction()
{
    perform query here
    if ($query->num_rows > 0
    {
        $id = $this->db->insert_id();
        return $id;
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB