문제

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();
도움이 되었습니까?

해결책

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!

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top