How to save same model twice in DB

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

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

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