Problem Record Multiple Rows With Isnewrecord

Hi,

I have checkboxlist with a list of name. I need one entry in the database by name selected.

My problem is, I only get the first checkbox or the last one.


{

		$model = Naskah::model()->find('id_naskah='.$id_naskah);

		if(! empty($model)&&isset($_POST['Naskah']))	

		{

			$model=new Naskah;						

			$model->no_referensi = $model->no;

			$model->attributes=$_POST['Naskah'];

			$tempCount = 0;

			$tempData = "";

			foreach($model->id_penerima as $checkbox_id)

			{

				$tempData[$tempCount]= $checkbox_id;

			}

			for($i=0;$i<count($tempData);$i++)

			{	

				$model->setIsNewRecord(true);

				$model->id_penerima = $tempData[$i];

				$model->lampiran=CUploadedFile::getInstance($model,'lampiran');

				if($model->save())

				{

					if($file_name=$model->lampiran)

					{

						$file_name->saveAs(dirname(Yii::app()->basePath) . '/files/'.$file_name);

						$this->redirect(array('sent'));

					}else

						$this->redirect(array('sent'));

				}

			}

where is wrong ? Thank you for your help and sorry for my weak english.

Actually setIsNewRecord() has almost no effect AFAIR, because it leaves PK value.

You probably set PK to null also.

thx for your response ORey.my PK is not null…

so what your suggest for my problem? I need one entry in one table by value checkbox selected but i only get the first checkbox or the last one. how to make all of it saving into multi rows with new id? please help :unsure:

either use $model = new YourModel or set PK to null every time before save.


foreach($model->id_penerima as $checkbox_id)

			{

				$tempData[$tempCount]= $checkbox_id;

			}

			for($i=0;$i<count($tempData);$i++)

			{	

				$model->isNewRecord=TRUE;

				$model->id_penerima = $tempData[$i];

				$model->id_naskah=NULL;

				if($model->save())

				{

					$model->lampiran=CUploadedFile::getInstance($model,'lampiran');

					if($file_name=$model->lampiran)

					{

						$file_name->saveAs(dirname(Yii::app()->basePath) . '/files/'.$file_name);

						$this->redirect(array('sent'));

					}else

						$this->redirect(array('sent'));

				}

			}

i already use PK to NULL…but why not worked? only one value has stored.have i got wrong?

Hi,

this will call the CHttpRequest::redirect(…) with $terminate=true and after that the Yii::app()->end() stop the running with an exit(…)

Try to redirect after the cycle:




$model=new Naskah;						

....

for($i=0;$i<count($tempData);$i++)

{	

    ....

}

$this->redirect(array('sent'));



Ok, let’s see.

First you find a record:


$model = Naskah::model()->find('id_naskah='.$id_naskah);

which is not safe btw, because of possible SQLi.

A couple of lines below you reset your $model by creating new instance:


$model=new Naskah;

Then you’re trying to access some data of that new instance (srsly?)


foreach($model->id_penerima)

storing some values in temp array, and then do another loop on that array (srsly??!)


for($i=0;$i<count($tempData);$i++)

and then do a redirect from inside the cycle (SRSLY??!!)

OMG MY EYES

Plz review your code line by line. How are you supposed to find errors when you have no idea what you’re doing?

You should assign


new Naskah

to another variable name.

Also, do want to find all of the entries or only one? If you want to find many entries you should use findAll

thank you very much…SOLVED