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? 回答1: I had to use this after the flush to get the last insert id: $em->persist($user); $em->flush(); $user->getId(); 回答2: You can access the id after calling the persist method of the entity manager. $widgetEntity = new WidgetEntity(); $entityManager->persist($widgetEntity); $entityManager->flush(); $widgetEntity->getId(); You do need to flush in order to get this