How to store multiple record at single execution




public function actionCreate()

	{

		$model=new PurchaseRecord;


               

		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['PurchaseRecord']))

		{

                        $insertion_query=array(); 

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

                        foreach ($model->things_id as $value)

                                $insertion_query[]="($model->purchase_id,$value)";

                        $insertion_query1='INSERT INTO purchase_record(purchase_id,things_id) VALUES'.implode(',', $insertion_query);

                       

                       if($model->dbConnection->createCommand("$insertion_query1")->execute())

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

		}


		$this->render('create',array(

			'model'=>$model,

		));

	}



I have a multiple select option for things so things id will have array of value

could i use like this or any good method available?

A loop?

I still haven’t solved this, Can any one Help Me?

Your question is a bit confusing…

you posted a code… and just asked: "could I use like this"…

Have you tried your code? Does it work? Do you get any error?

Ya i tried it and it works fine, Im asking is it a good practice to do like this? or any good method to do this work

As nobody answered, you can assume that nobody got a similar need… so there is no practice for this :)

As I see it… If it works… than it’s OK…

One other solution would be to create a new model… assign the value and use $model->save(), but it would be much, much slower than this solution… so for your practical need this is just fine…