Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZF2 How to get last insert id value?

I am stuck with getting last insert id with Zend framework 2 and I gave up on this...

There are tried combinations:

var_dump($this->tableGateway->insert($insert));
var_dump($this->tableGateway->lastInsertValue);
var_dump($this->tableGateway->getLastInsertValue());
var_dump($this->tableGateway->getAdapter()->getDriver()->getConnection()->getLastGeneratedValue());

Value is inserting to table, but every line (except first, which gives int "1") returns null. Please don't tell me, that such a big framework does not give possibility to get last insert id value!?

like image 442
Piotr Avatar asked Apr 09 '13 17:04

Piotr


People also ask

How can I get last insert ID in PDO?

You can get the id of the last transaction by running lastInsertId() method on the connection object($conn).

What does last insert ID return?

The LAST_INSERT_ID() function returns the AUTO_INCREMENT id of the last row that has been inserted or updated in a table.

How do I find the last ID of a database?

If you are AUTO_INCREMENT with column, then you can use last_insert_id() method. This method gets the ID of the last inserted record in MySQL. Insert some records in the table using insert command.


Video Answer


1 Answers

Here is what I use:

$data = array()// Your data to be saved;
$this->tableGateway->insert($data);
$id = $this->tableGateway->lastInsertValue;
like image 92
Jean Paul Avatar answered Oct 19 '22 11:10

Jean Paul