How to get last inserted id in yii2 using createCommand?
36,976
Solution 1
Yii::$app->db->createCommand($sql)->execute();
Then call function getLastInsertID,
$id = Yii::$app->db->getLastInsertID();
Solution 2
You can do this using:
$lastInsertID = $db->getLastInsertID();
echo $lastInsertID;
Author by
Rahul
Updated on July 29, 2022Comments
-
Rahul almost 2 years
I am using
yii2
php framework. I want insert record into database usingtransaction
. How can I get last inserted id usingcreateCommand()
.Please check following code,
$db = Yii::$app->db; $sql = $db->createCommand()->insert('user', [ 'name' => 'test', 'email_address' => '[email protected]', 'phone_number' => '432432424', ])->execute();