Zend Framework: How to retrieve the id of the last inserted row?

Andrew picture Andrew · Dec 8, 2009 · Viewed 30.1k times · Source

I'm inserting a new row into my database with this code:

$data = array(
    'key' => 'value'
);
$this->getDbTable()->insert($data);

How can I get the row id of the this row that I just created?

Answer

Amit Dugar picture Amit Dugar · Jun 22, 2011

Did you try this ? This also works fine.

//just after you call your insert($data) function .. use this
$lastInsertId = $this->getAdapter()->lastInsertId();