Reputation: 25
When I try to use the function '->lastInsertId()' to retrieve the lat ID of a table I get back '0'. I can't find the solution. My table is an autoincrement
I try to get it in the controller with this code.
$reviews = new Application_Model_DbTable_Reviews();
$lastId = $reviews->getAdapter()->lastInsertId();
echo $lastId;
I hope someone can help me.
With kind regards,
Nick
Upvotes: 2
Views: 167
Reputation: 8196
Well this stuff is not mention in docs but it works for e.g if you have table name 'Book' with PK book_id , FK user_id and 'User' table with PK user_id
<<Book>>
*book_id
title
user_id
<<User>>
*user_id
name
age
then
$userTb = new Model_DbTable_User();
$user = $userTb->createRow();
$user->name = "jason";
$user->age = 25;
$user->save();
//well after saving the record ZF populates PK for you so now you have read only access to auto incremented PK simply by $userTb->user_id;
so
$bookTb = new Model_DbTable_Book();
$book = $bookTb->createRow();
$book->title = 'php';
$book->user_id = $user->user_id;
$bookId = $book->save(); // this is another way of accessing auto generated PK at insert tim .
Upvotes: 1