Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii : Getting id after createCommand execute query

Tags:

sql

php

yii

In active record, I usually just get

$model->id, 

after

executing $model->save();

But how about createCommand? how do I get the id (pk) of the inserted row?

Yii::app()->db->createCommand($sql)->query();

I tried: $id = Yii::app()->db->getLastInsertedID();

but it asks for sequence name. error. How do I do the active record counter part? Thanks!

like image 374
muffin Avatar asked Jun 16 '14 04:06

muffin


1 Answers

If you go with execute() instead of query()

 Yii::app()->db->createCommand($sql)->execute();

then you can use

 $id = Yii::app()->db->getLastInsertID();

to get the Id of the newly inserted record.

You can read more here.

like image 193
Developerium Avatar answered Oct 15 '22 02:10

Developerium