2

Is there a way after the ->save() call to get the last insert id?

Example Code:

$post_data = $_POST;
$user = ORM::factory('user');
$user->username = $post_data['username'];
$user->email = $post_data['email'];
$user->save();
12Bo
  • 116
  • 1
  • 3
  • 11

2 Answers2

3

Of course, assuming your insert code looked like this:

$user = ORM::factory('user')->values($post)->save();

To get the last insert ID simply do this after the call to ->save()

echo $user->id;

In your case you would need to do $user->user_id as you've named your primary key user_id.

I would alternatively recommend biakaveron's advice to instead use $user->pk() as it will always return the value of the primary key regardless of the name given to it, provided that you specify the primary key name in your model with $_primary_key.

The model will be populated with the saved values ready for reuse if the insert worked.

Job done!

David Hancock
  • 14,861
  • 4
  • 41
  • 44
1

Just call $model->id; - ORM will do the last insert id inclusion for you.

Kemo
  • 6,942
  • 3
  • 32
  • 39