I am using transactions in Magento.
I need to use primeryKey of first insert query to all my subsequent queries.
$model1->setfield1()
->setField2();
$transaction->addObject($model1);
$connection = Mage::getSingleton('core/resource')->getConnection('core_read');
$lastInsertId = $connection->fetchOne('SELECT last_insert_id()'); // return 0
$model2->setfield3($lastInsertId )
$model3->setfield4($lastInsertId )
$transaction->addObject($model2);
$transaction->addObject($model3);
$transaction-Save();
$lastInsertId2 = $connection->fetchOne('SELECT last_insert_id()'); // returns correct result
how to get last inserted id before saving the transaction
I guess PDO (which magento uses) can’t get last_inserted_id while transaction isn’t commited yet. I guess you should either try plain sql like there or try use nested transactions.