One controller and Multiple Tables with Array values

Please help me with the following code. Model2 is not saving values to database.





public function actionCreate()

	{	

            $model = new SampleSubmissionCustomerForm;

            //$model2 = new SampleProduct;


		// Uncomment the following line if AJAX validation is needed

		$this->performAjaxValidation($model,"samplesubmissioncustomerform-create-form");

            if(Yii::app()->request->isAjaxRequest)

	       {

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

		    {				

			    

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

			$model->lc_number=$_POST['SampleSubmissionCustomerForm']['lc_number'];

            $model->product_id="";

            $model->quantity="";

            $model->unit="";

            $model->manufacturer_date="";

            $model->expire_date="";

            $model->retest_date="";

            $model->batch_no="";

            $model->dosage="";

            $model->packing="";

            $model->application="";


			  if($model->save())

			    {

			  foreach($_POST['SampleSubmissionCustomerForm']['product_id'] as $key=>$product):

				if($product)

			{

			$model2=new SampleSubmissionItem;

			$model2->sample_id=$model->id;

			$model2->product_id=$product;

			$model2->quantity=$_POST['SampleSubmissionCustomerForm']['quantity'][$key];

			$model2->unit=$_POST['SampleSubmissionCustomerForm']['unit'][$key];


			$model2->manufacturer_date=$_POST['SampleSubmissionCustomerForm']['manufacturer_date'][$key];

			$model2->expire_date=$_POST['SampleSubmissionCustomerForm']['expire_date'][$key];

			$model2->retest_date=$_POST['SampleSubmissionCustomerForm']['retest_date'][$key];

			$model2->batch_no=$_POST['SampleSubmissionCustomerForm']['batch_no'][$key];

			$model2->dosage=$_POST['SampleSubmissionCustomerForm']['dosage'][$key];

			$model2->packing=$_POST['SampleSubmissionCustomerForm']['packing'][$key];

			$model2->application=$_POST['SampleSubmissionCustomerForm']['application'][$key];


			$model2->save();

					}

			 endforeach;

			      echo $model->id;

			    }

			 else

			    {

			      echo "false";

			    } 

			 return;

		    }

	       }

	       else

	       {

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

		    {

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

			$model->lc_number=$_POST['SampleSubmissionCustomerForm']['lc_number'];

            $model->product_id="";

            $model->quantity="";

            $model->unit="";

            $model->manufacturer_date="";

            $model->expire_date="";

            $model->retest_date="";

            $model->batch_no="";

            $model->dosage="";

            $model->packing="";

            $model->application="";

			  if($model->save())

                {

              foreach($_POST['SampleSubmissionCustomerForm']['product_id'] as $key=>$product):

                    if($product)

                    {

            $model2=new SampleSubmissionItem;

			$model2->sample_id=$model->id;

			$model2->product_id=$product;

			$model2->quantity=$_POST['SampleSubmissionCustomerForm']['quantity'][$key];

			$model2->unit=$_POST['SampleSubmissionCustomerForm']['unit'][$key];


			$model2->manufacturer_date=$_POST['SampleSubmissionCustomerForm']['manufacturer_date'][$key];

			$model2->expire_date=$_POST['SampleSubmissionCustomerForm']['expire_date'][$key];

			$model2->retest_date=$_POST['SampleSubmissionCustomerForm']['retest_date'][$key];

			$model2->batch_no=$_POST['SampleSubmissionCustomerForm']['batch_no'][$key];

			$model2->dosage=$_POST['SampleSubmissionCustomerForm']['dosage'][$key];

			$model2->packing=$_POST['SampleSubmissionCustomerForm']['packing'][$key];

			$model2->application=$_POST['SampleSubmissionCustomerForm']['application'][$key];


                 $model2->save();

                       }

                endforeach;

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

                }

		

		    }

               

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

			    'model'=>$model, 

		    ));

	       }	

	}



are you using yii2 or yii1.1 ?

Could be that you have some validation errors and the model does not save… did you check if your models validate?

I am using yii1.1.

Model data are saved. but Model2 is not saving…

Where can I put the validation code in my coding…please need your expert opinion. If you want I can submit Model and View coding also.

[color="#006400"]NOTE: moved to proper section (instead of Yii 2.0 - General Discussion)[/color]

$model2->save() does the validation

Just to check this idea you [size=2]can try it like if($model2->save()) …[/size]

and in the else output $model->getErrors()

I am attaching the screenshot.

I think it might help you to understand my problem.

Following table Data is for Model2…[SampleSubmissionItem].







<td><?php echo $form->dropDownListRow($model,'product_id[]',

               CHtml::listData(Product::model()->findAll(), 'id', 'name'),array('onChange'=>CHtml::ajax(array(

                                        'url' => CController::createUrl('getproducts'),

                                       'type' => 'POST',

                                        'update'=>'.fff',

                                    )),'empty' => 'Select Supplier','class'=>"span1 bbb",

                                    'placeholder'=>strip_tags($form->label($model,'product_id')),

                                    'labelOptions' => array('label' => false)));      ?></td>        

        

        <td><?php echo $form->textFieldRow($model,'quantity[]',array('class'=>"span1",'placeholder'=>strip_tags($form->label($model,'quantity')),'labelOptions' => array('label' => false))); ?></td>

        <td><?php echo $form->textFieldRow($model,'unit[]',array('class'=>"span1",'placeholder'=>strip_tags($form->label($model,'unit')),'labelOptions' => array('label' => false))); ?></td>

        <td><?php echo $form->textFieldRow($model,'manufacturer_date[]',array('class'=>"span1",'placeholder'=>strip_tags($form->label($model,'manufacturer_date')),'labelOptions' => array('label' => false))); ?></td>

        <td><?php echo $form->textFieldRow($model,'expire_date[]',array('class'=>"span1",'placeholder'=>strip_tags($form->label($model,'expire_date')),'labelOptions' => array('label' => false))); ?></td>

        <td><?php echo $form->textFieldRow($model,'retest_date[]',array('class'=>"span1",'placeholder'=>strip_tags($form->label($model,'retest_date')),'labelOptions' => array('label' => false))); ?></td>

        <td><?php echo $form->textFieldRow($model,'batch_no[]',array('class'=>"span1",'placeholder'=>strip_tags($form->label($model,'batch_no')),'labelOptions' => array('label' => false))); ?></td>

        <td><?php echo $form->textFieldRow($model,'dosage[]',array('class'=>"span1",'placeholder'=>strip_tags($form->label($model,'dosage')),'labelOptions' => array('label' => false))); ?></td>

        <td><?php echo $form->textFieldRow($model,'packing[]',array('class'=>"span1",'placeholder'=>strip_tags($form->label($model,'packing')),'labelOptions' => array('label' => false))); ?></td>




        <td><?php echo $form->textFieldRow($model,'application[]',array('class'=>"span1",'placeholder'=>strip_tags($form->label($model,'application')),'labelOptions' => array('label' => false))); ?></td>

       <td style="vertical-align:middle !important;"><a href="#" class="" onclick="nnn()"><i class="icon-plus"></i></a>

                                        <a href="#" class="delete1"><i class="icon-minus"></i></a></td>