Retrieving new record id in transaction


Perhaps a noob question, but I didn’t find any answers around.

When I save model inside transaction, how do I retrieve (if possible at all) new record id before committing?

My code:

$transaction = Yii::app()->db->beginTransaction();


try {


$new_id = $model->id;


$something->something = $new_id;



}catch(Exception $e){



Unfortunately, this does not work - $model->id is empty.

Please enlighten me - what am I missing.

Thanks in advance!




$new_id = $model->id;


Yes that solved it! Thanks. You are a star, mister!

Is this somewhere in documentation and I missed it or is it a knowledge accumulated by experience? :D

Glad it helped. Well I don’t remember, I certainly needed that as well and searched for it. I may have found it in the forum, or the blog tutorial, or Larry Ullman’s series.