Using an auto-increment id during the creation of a record

When i create a record, im copying the id of the record to another field, but since it’s auto-increment i cant know the id before creating the record ( i think ), so im doing something like this

public function actionCreate()


$model=new Car;




  $model->newcolor = $model->id;   




It looks like an ugly workaround but it works ;D , so is there a better way of doing this ?

Hope this will help you.

$command = Yii::app()->db->createCommand('SHOW TABLE STATUS LIKE "<your table name>"');



Is there no redundancy, to store the same value in the same table twice?

If you really need it, I think I’d do almost the same, during one transaction saving a model and then updating it with the value.

There’re two discussions at stackoverflow that may be useful:



yes there is a redundancy…check it blaco.