Domanda

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();
È stato utile?

Soluzione

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!

Altri suggerimenti

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top