blaco
(Blaco)
July 16, 2012, 12:12am
1
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 , so is there a better way of doing this ?
aruna470
(Aruna470)
July 16, 2012, 12:24am
2
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'];
yugenekr
(Yugenekr)
July 17, 2012, 2:59am
3
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.