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!
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.
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