2017-12-07 10 views
1

의 마지막 삽입 이드 Symfony2의 doctrine2 쿼리Symfony2의 doctrine2 쿼리

$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"; 
    } 

하지만 난 쿼리 아래 썼을 때 출력이 비어 오는 마지막 삽입 ID를 가져옵니다.

var_dump($connection->lastInsertId()); 
+1

변화 : -' $ Querystatement->) (실행,''에 경우 ($ Querystatement-> 실행()) {$ 연결 -> lastInsertId();} 다른 {에코 "는 쿼리가 실행되지"; }'and check –

+0

예 코드를 업데이트했지만 출력이 'bool (false)'인 경우 –

+0

죄송합니다. if 조건 내에서 var_dump ($ connection-> lastInsertId());를 수행합니다. –

답변

0

아래 코드를 사용해보십시오.

$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'];