Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the last insert id with doctrine 2?

How can I get the last insert id with doctrine 2 ORM? I didn't find this in the documentation of doctrine, is this even possible?

like image 588
tom Avatar asked Aug 18 '10 06:08

tom


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).

How can I get the last inserted ID from a table in SQL?

To get an ID of last inserted record, you can use this T-SQL: INSERT INTO Persons (FirstName) VALUES ('Joe'); SELECT ID AS LastID FROM Persons WHERE ID = @@Identity; You can use query like this inside stored procedure or as an ad-hoc query.

How do I get the last inserted id in MySQL?

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.

How can I get insert id?

Get ID of The Last Inserted RecordIf we perform an INSERT or UPDATE on a table with an AUTO_INCREMENT field, we can get the ID of the last inserted/updated record immediately.


1 Answers

I had to use this after the flush to get the last insert id:

$em->persist($user); $em->flush(); $user->getId(); 
like image 77
tom Avatar answered Sep 21 '22 16:09

tom