Get the last insert id with doctrine 2?

PhpOrmDoctrineLastinsertid

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

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

$em->persist($user);
$em->flush();
$user->getId();

Solution 2 - Php

You can access the id after calling the persist and flush methods of the entity manager.

$widgetEntity = new WidgetEntity();
$entityManager->persist($widgetEntity);
$entityManager->flush();
$widgetEntity->getId();

You must call flush in order to get this id.

Solution 3 - Php

If you're not using entities but Native SQL as shown here then you might want to get the last inserted id as shown below:

$entityManager->getConnection()->lastInsertId()

For databases with sequences such as PostgreSQL please note that you can provide the sequence name as the first parameter of the lastInsertId method.

$entityManager->getConnection()->lastInsertId($seqName = 'my_sequence')

For more information take a look at the code on GitHub here and here.

Solution 4 - Php

Calling flush() can potentially add lots of new entities, so there isnt really the notion of "lastInsertId". However Doctrine will populate the identity fields whenever one is generated, so accessing the id field after calling flush will always contain the ID of a newly "persisted" entity.

Solution 5 - Php

A bit late to answer the question. But,

If it's a MySQL database

should $doctrine_record_object->id work if AUTO_INCREMENT is defined in database and in your table definition.

Solution 6 - Php

In my case, as I declared $id as private I just created a new public method to get it.

public function getId() {
 return $this->id;
}

So then I could call it like so

    $user = new User();
    $user->setUsername("Kylo Ren");
    $entityManager = getEntityManager();
    $entityManager->persist($user);
    $entityManager->flush();
    echo "Created User with ID " . $user->getId() . "\n";

Solution 7 - Php

Here i post my code, after i have pushed myself for one working day to find this solution.

Function to get the last saved record :

private function getLastId($query) {
        $conn = $this->getDoctrine()->getConnection();
        $stmt = $conn->prepare($query);
        $stmt->execute();
        $lastId = $stmt->fetch()['id'];
        return $lastId;
    }

Another Function which call the above function

private function clientNum() {
        $lastId = $this->getLastId("SELECT id FROM client ORDER BY id DESC LIMIT 1");
        $noClient = 'C' . sprintf("%06d", $lastId + 1); // C000002 if the last record ID is 1
        return $noClient;
    }

Solution 8 - Php

More simple: SELECT max(id) FROM client

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontomView Question on Stackoverflow
Solution 1 - PhptomView Answer on Stackoverflow
Solution 2 - PhpclarkstachioView Answer on Stackoverflow
Solution 3 - PhpFrancesco CasulaView Answer on Stackoverflow
Solution 4 - PhpbeberleiView Answer on Stackoverflow
Solution 5 - PhpPurasapaView Answer on Stackoverflow
Solution 6 - PhpkikemnzzView Answer on Stackoverflow
Solution 7 - PhpTechappriView Answer on Stackoverflow
Solution 8 - PhpOlivier BussierView Answer on Stackoverflow