CodeIgniter Forums
Return last inserted 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: Return last inserted ID (/showthread.php?tid=20944)



Return last inserted ID - El Forum - 07-26-2009

[eluser]NateL[/eluser]
In my view, a user creates a new user.

My controller accepts the data, and passes it on to the model, where the model then inserts it into the database.

How do I then return that ID that was just created into the Controller?

PHP has the mysql_insert_id() function, but I'm not quite sure where to put it, or how to get it back into the Controller.

Thanks


Return last inserted ID - El Forum - 07-26-2009

[eluser]LuckyFella73[/eluser]
If you use the active record class you could return the
id from your model:

Code:
# in your controller
$insert_id = $this->your_model->db_function();


# in your model
// your db-insert stuff here
// then in the end of the models function you called:
return $this->db->insert_id();



Return last inserted ID - El Forum - 07-26-2009

[eluser]NateL[/eluser]
Alright,

my controller sends the posted data to the model, like so:

Code:
function add()
{
$this->addressbook_model->add_new_address($_POST);
}

My model processes that data and sticks it in the database:

Code:
function add_new_address()
{
    $data = array(
        'name'      => $this->input->post('newname'),
        'email'     => $this->input->post('newemail')

    );
        
    $this->db->escape($data);
    $this->db->insert('addressbook', $data);
    $data['lastid'] = $this->db->insert_id();
    return $data;
}

As you can see, the array $data contains name and email - but after the query, it contains a third slot called 'lastid' with an integer (this works...tested it), and returns the entire $data array to my controller....

Now, how do I access that returned data? If I just use $data['lastid'] in my controller, it gives me an error.


Return last inserted ID - El Forum - 07-26-2009

[eluser]LuckyFella73[/eluser]
As far as I know you can access the last insert id from controller
direcly (after calling the model) without returning the id from
your model.
Code:
# controller:

// call you model
$this->addressbook_model->add_new_address();
// you don't need to write "$_POST" here
// POST data is available in your model

// and then (still controller)
$insert_id = $this->db->insert_id();

Is there a special reason why you try to return the
post data from your model back to tha controller? I mean, the post
data is available anyway.


Return last inserted ID - El Forum - 07-26-2009

[eluser]David Johansson[/eluser]
[quote author="NateL" date="1248655160"]Alright,

my controller sends the posted data to the model, like so:

Code:
function add()
{
$this->addressbook_model->add_new_address($_POST);
}

My model processes that data and sticks it in the database:

Code:
function add_new_address()
{
    $data = array(
        'name'      => $this->input->post('newname'),
        'email'     => $this->input->post('newemail')

    );
        
    $this->db->escape($data);
    $this->db->insert('addressbook', $data);
    $data['lastid'] = $this->db->insert_id();
    return $data;
}

As you can see, the array $data contains name and email - but after the query, it contains a third slot called 'lastid' with an integer (this works...tested it), and returns the entire $data array to my controller....

Now, how do I access that returned data? If I just use $data['lastid'] in my controller, it gives me an error.[/quote]


You should call the function like:
Code:
function add()
{
$data = $this->addressbook_model->add_new_address();
}
You don't need to pass the $_POST and you have to save the return data to you variable $data


Return last inserted ID - El Forum - 07-27-2009

[eluser]NateL[/eluser]
Thanks David,

So, by storing $this->addressbook_model->add_new_address(); in the variable $data - CI will pass the posted data to my addressbook_model?

Also, should I clean and format the data in the Controller or in the model?


Return last inserted ID - El Forum - 07-27-2009

[eluser]David Johansson[/eluser]
[quote author="NateL" date="1248724479"]1# So, by storing $this->addressbook_model->add_new_address(); in the variable $data - CI will pass the posted data to my addressbook_model?

2# Also, should I clean and format the data in the Controller or in the model?[/quote]

1# The posted data is availible within the CI control, the views and the models by default.

2# Both will work, but cleaning the data in the controller is probably more MVC compliant.

Actually it's probably better not to access the post variables within the model but to do that in the controller and then store the data in one or more strings or arrays to pass to the model. Like:

Code:
$address['line_1'] = $this->input->post('line_1');
$address['line_2'] = $this->input->post('line_2');
$address['zip_code'] = $this->input->post('zip_code');
$address['state'] = $this->input->post('state');

$this->addressbook_model->add_new_address($address);

Before that you might want to do some form validation...


Return last inserted ID - El Forum - 07-27-2009

[eluser]NateL[/eluser]
Very nice - Thanks Smile