user3702874 user3702874 - 1 year ago 155
PHP Question

How to detect the last insert ID within a transaction in Yii using DAO?

That's the source code, I need to detect the ID (see the marked position between the two queries below).

$connection = Yii::app()->db;
try {

$q = "INSERT INTO `someTable1` .... ";
$connection->createCommand($q)->execute(); // Single Row Inserted

// HERE!! How to get the last insert ID from query above

$q = "INSERT INTO `someTable2` ....


} catch (Exception $e) {
// react on exception

What would be the most suitable way to do that?

Answer Source
$lastInsertID = $connection->getLastInsertID();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download