I am using yii2
php framework. I want insert record into database using transaction
. How can I get last inserted id using createCommand()
.
Please check following code,
$db = Yii::$app->db;
$sql = $db->createCommand()->insert('user', [
'name' => 'test',
'email_address' => '[email protected]',
'phone_number' => '432432424',
])->execute();
Yii::$app->db->createCommand($sql)->execute();
Then call function getLastInsertID,
$id = Yii::$app->db->getLastInsertID();
You can do this using:
$lastInsertID = $db->getLastInsertID();
echo $lastInsertID;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With