Retrieving new record id in transaction

Hi,

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 {

$model->save();

$new_id = $model->id;

...

$something->something = $new_id;

$something->save();

$transaction->commit();


}catch(Exception $e){

$transaction->rollback();

}




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

Please enlighten me - what am I missing.

Thanks in advance!




…

$model->save();

$model->refresh();

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