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;

if(isset($_POST[‘Car’]))

{

  $model->save();


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


  $model->save();

}

}

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>"');

$res=$command->queryRow();	

$next_id=$res['Auto_increment'];



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:

http://stackoverflow…-inserting-data

http://stackoverflow…n-one-statement

yes there is a redundancy…check it blaco.