Hey guys,
I want to save each model twice in my DB.
Here is my ControllerCode:
public function actionCreate($id)
{
$model=new pm;
if(isset($_POST['pm']))
{
$model->attributes=$_POST['pm'];
// 1. Save
$model->save();
// 2. Save
$model->read = 1;
if($model->save())
$this->redirect(array('view','id'=>$model->idPm));
}
$this->render('create',array(
'model'=>$model,
));
}
For some reason the model is saved only once, eventhough I called $model->save() twice
THX
Andreas
mdomba
(Maurizio Domba Cerin)
May 31, 2011, 8:00pm
2
check the documentation for save() - http://www.yiiframework.com/doc/api/1.1/CActiveRecord#save-detail
the record is inserted if the properta isNewRecord is true… the first time you save the record this properties is set to false… so the second time save() updates the record…
try to set $model->isNewRecord to false before calling save() the second time…
mdomba:
check the documentation for save() - http://www.yiiframework.com/doc/api/1.1/CActiveRecord#save-detail
the record is inserted if the properta isNewRecord is true… the first time you save the record this properties is set to false… so the second time save() updates the record…
try to set $model->isNewRecord to false before calling save() the second time…
Hmm when doing
$model->isNewRecord = true;
I get the error:
Integrity constraint violation: 1062 Duplicate entry '40' for key 'PRIMARY'
That means I have to increase the pk manually…
Is there a better way?
THX
rurbinac
(Rurbinac)
June 1, 2011, 2:07am
4
Andreas123:
Hmm when doing
$model->isNewRecord = true;
I get the error:
Integrity constraint violation: 1062 Duplicate entry '40' for key 'PRIMARY'
That means I have to increase the pk manually…
Is there a better way?
THX
Never done that, but try something like
$model->isNewRecord = true
unset($model->pk); // or $model->pk = NULL (or FALSE)
Because it looks like is trying to INSERT again but reusing the same PK