這裏行的ID是我的代碼:的Symfony2 /學說 - 獲取其即將被插入
$cost = new Cost();
if(some_condition){
$cost->setColumn1('some_value');
} else if(some_condition){
$cost->setColumn2('some_value');
/**How do I get the ID of the cost row that is about to be inserted here**/
} else if(some_condition){
$cost->setColumn3('some_value');
}
$em->persist($cost);
$em->flush();
我想這樣做的:
$cost = new Cost();
if(some_condition){
$cost->setColumn1('some_value');
} else if(some_condition){
$cost->setColumn2('some_value');
$em->persist($cost);
$em->flush();
/**How do I get the ID of the cost row that is about to be inserted here**/
$cost->getId();
} else if(some_condition){
$cost->setColumn3('some_value');
}
$em->persist($cost);
$em->flush();
但由於成本$對象堅持和衝兩次,這行不會被插入兩次?我該如何解決這個問題?有沒有更聰明的方法來做到這一點?
成本表中的id字段是自動遞增的。所以除非數據被插入到數據庫中,否則id不可用。 – aBhijit