Insert multiple record in a table

Hi,

can anybody tell me how to insert multiple records of data in a table.? I am very new at this an d would like if anybody could give me an example.

I have 2 tables product and subproduct.

Product has the fields product_id, prod_type, quantity

subproduct the fields subproduct_id, product_id, subproduct_name.(Note: a product has many subproducts)

So far i am able to make inserts in the product table and in the subproduct table i am able to insert the FK(product_id) but not able to insert the subproduct_name.

Please tell me how to insert the multiple subproducts in the table.

The following is my actioncreate of the products controller

public function actionCreate()

{


	$model=new Products;


     $model2=new SubProducts;


	// Uncomment the following line if AJAX validation is needed


	 $this->performAjaxValidation($model);





	if(isset($_POST[Products]))


	{


		$model->attributes=$_POST[Products];


			$model2->attributes=$_POST[SubProducts];


		


		if($model->save())


			{


				$model2->products_id = $model->products_id;


			


				


				$model2->save();  


			$this->redirect(array('view','id'=>$model->products_id));


	}


	}





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


		'model'=>$model,


		'model2'=>$model2,


	));


}

Any help would be really appreciated. Thanks

A lot of problems with inserting records come from the ‘rules’ in the associated models.

If you have attributes with no rules, even that can prevent creating the record.

Have you tried entering dummy values? Example




$model2->products_id = $model->products_id;

$model2->name = 'HelloWorld';

$model2->price ='123456';



This way you can enter values that should work. You can also capture the $_POST data and display it on your screen. Perhaps your SubProducts array is empty?




if(isset($_POST[Products]))

{

echo '<pre>'; // keeps formating of array

print_r($_POST); // display the entire array

die; // terminate


$model->attributes=$_POST[Products];



Oh and if you are new to the forum try using the code tags (icon looks like this <>), it makes it a little easier to look at code snippets.

doodle

Thanks for the info doodle. It did help.

By setting that attribute (those who do not have a specific rul as ‘safe’ is enough.

If that is an exact copy of your code, please make sure you double check for ‘both’ the ‘posted’ form fields




if(isset($_POST[Products])) // <<<-- $_POST["Products"] (between double or single quotes)

{

$model->attributes=$_POST[Products];

$model2->attributes=$_POST[SubProducts]; // <<<--- you are not checking if is 'posted'? (double or single quotes missing too)


if($model->save()) // <<<--- after Save... -

{

$model2->products_id = $model->products_id; // <<<--- setting products id relation... 

$model2->save();  // <------------- not checking if it is successfully saved (make sure you do and if not good check its errors! Do not redirect)

// you are redirecting here without consulting whether the second model has been

// saved or not, thus not knowing if there was any errors.... 

// change this and then tell us the errors that display

$this->redirect(array('view','id'=>$model->products_id));

}

}




IMHO, I would consider reviewing the code, and double check every step, that will help you debug your code a bit further.

Cheers