Snopzer
Snopzer

Reputation: 1702

Get the Last insert Id in Symfony2 doctrine2 query

Get the Last insert Id in Symfony2 doctrine2 query

    $apptQuery = "insert into tbl_student_scores (test_date,updated_by_id) values(:testDate,:loggedinUser)";
    $em = $this->getDoctrine()->getEntityManager();
    $connection = $em->getConnection();
    $Querystatement = $connection->prepare($apptQuery);
    $Querystatement->bindValue('testDate', $test->test_date);
    $Querystatement->bindValue('loggedinUser', $loggedinUser);
    if($Querystatement->execute()){
           var_dump($connection->lastInsertId()); // control coming here but the outpur is bool(false)
    }else{
            echo "query not executed"; 
    }

but the output coming as empty when i wrote below query.

var_dump($connection->lastInsertId());

Upvotes: 1

Views: 289

Answers (1)

Mohammad Fareed
Mohammad Fareed

Reputation: 1972

Try the Below Code.

$apptQuery = "insert into tbl_student_scores (test_date,updated_by_id) values(:testDate,:loggedinUser)";
$em = $this->getDoctrine()->getEntityManager();
$connection = $em->getConnection();
$Querystatement = $connection->prepare($apptQuery);
$Querystatement->bindValue('testDate', $test->test_date);
$Querystatement->bindValue('loggedinUser', $loggedinUser);
$Querystatement->execute();
$insertID = $Querystatement->fetchAll();
$scoreId = $insertID[0]['id'];

Upvotes: 1

Related Questions