yii Undefined index: product_id

Hi, friends

My purpose is create dynamic product and quantity text box then loop and save it, but I got some mistakes where i didn’t know something wrong. I got an error message said that undefined index product_id. Please help to find solution and how controller does that action.

i have four models: Product,Customer,Order and OrderDetail.

Thanks in advance.

view/order/_form.php





<div class="form">


<?php $form=$this->beginWidget('CActiveForm', array(

	'id'=>'order-form',

	'enableAjaxValidation'=>false,

)); ?>


	<p class="note">Fields with <span class="required">*</span> are required.</p>


	<?php echo $form->errorSummary($model); ?>


	<div class="row">

		<?php echo $form->labelEx($model,'customer_id'); ?>

		<?php //echo $form->textField($model,'customer_id'); ?>

		<?php echo $form->dropDownList($model,'customer_id',CHtml::listData(Customers::model()->findAll(),'customer_id','fullname'),

	   array('empty' => '--- Choose---')); ?>

		<?php echo $form->error($model,'customer_id'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'order_status'); ?>

		<?php echo $form->textField($model,'order_status'); ?>

<?php //echo $form->dropDownList($model,'order_id', array(1=>'Pending', 2=>'Processing',3=>'Completed'));?>


		<?php echo $form->error($model,'order_status'); ?>

	</div>

	

<?php /*?>

	<div class="row">

		<?php echo $form->labelEx($model,'lat'); ?>

		<?php echo $form->textField($model,'lat'); ?>

		<?php echo $form->error($model,'lat'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'lng'); ?>

		<?php echo $form->textField($model,'lng'); ?>

		<?php echo $form->error($model,'lng'); ?>

	</div>

<?php */?>


	<div class="row">

		<?php echo $form->labelEx($model,'address'); ?>

		<?php echo $form->textField($model,'address',array('size'=>60,'maxlength'=>255)); ?>

		<?php echo $form->error($model,'address'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'staff_id'); ?>

		<?php //echo $form->textField($model,'staff_id'); ?>

		<?php echo $form->dropDownList($model,'staff_id',CHtml::listData(Staff::model()->findAll(),'staff_id','fullname'),

	   array('empty' => '--- Choose---')); ?>

		<?php echo $form->error($model,'staff_id'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'received_date'); ?>

		<?php echo $form->textField($model,'received_date'); ?>

		<?php echo $form->error($model,'received_date'); ?>

	</div>


<script type="text/javascript">


 function addProduct() {

  var ni = document.getElementById('divForPhoto');

  var numi = document.getElementById('countLastInput');


  var num = (document.getElementById('countLastInput').value -1)+ 2;


  numi.value = num;


  var newdiv = document.createElement('div');


  var divIdName = num;


  newdiv.setAttribute('id',divIdName);


  newdiv.innerHTML = <?php echo $form->dropDownList($orderdetail,'product_id[]',CHtml::listData(Products::model()->findAll(),'product_id','product_name'),array('empty' => '--- Choose---')); ?>+<?php echo $form->textField($orderdetail,'qty[]'); ?>+'<a href="#" onclick="removePhoto('+divIdName+')">Remove</a>';


  ni.appendChild(newdiv);


}

 function removePhoto(divNum) {


	  var d = document.getElementById('divForPhoto');


	  var olddiv = document.getElementById(divNum);

	  d.removeChild(olddiv);

		var minus=document.getElementById('countLastInput').value;

		document.getElementById('countLastInput').value=minus-1;

	}

	 

</script>


	

<?php /* ?><form action="php_multiple_textbox4.php" method="post" name="form1">  

<input type="text" name="txtSiteName[]">  

<input name="btnButton" type="button" value="+" onClick="JavaScript:fncCreateElement();"><br>  

<span id="mySpan"></span>  

<input name="btnSubmit" type="submit" value="Submit">  

</form> 

<?php */?>




	

<?php /*?>

	<div class="row">

		<?php echo $form->labelEx($model,'completed_date'); ?>

		<?php echo $form->textField($model,'completed_date'); ?>

		<?php echo $form->error($model,'completed_date'); ?>

	</div>

<?php */?>


	

<?php /*?>

	<div class="row">

		<?php echo $form->labelEx($model,'created_date'); ?>

		<?php echo $form->textField($model,'created_date'); ?>

		<?php echo $form->error($model,'created_date'); ?>

	</div>


	<div class="row">

		<?php echo $form->labelEx($model,'upated_date'); ?>

		<?php echo $form->textField($model,'upated_date'); ?>

		<?php echo $form->error($model,'upated_date'); ?>

	</div>

<?php */?>

<?php /*?>

<table id="students">

    <thead>

        <tr>

            <td>Product Name</td>

            <td>Quantity</td>

            <td>

                <?php echo CHtml::link('<b>Add Product</b>', '', array('onClick'=>'addProduct($(this))', 'class'=>'add'));?>

            </td>

        </tr>

    </thead>


</table>

 

<input type="hidden" value="0" id=lastInput name="lastInput" />

<?php */?>

<div id="divForPhoto"> </div>    

<input type="button" name="service_photo" value="Add Product" class="Allbutton" onclick="addProduct();" />

<input type="hidden" value="0" id="countLastInput" name="countLastInput" />

	

	<div class="row buttons">

		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>

	</div>


<?php $this->endWidget(); ?>


</div><!-- form -->

 

OrderController.php




<?php


class OrderController extends Controller

{

	/**

	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning

	 * using two-column layout. See 'protected/views/layouts/column2.php'.

	 */

	public $layout='//layouts/column2';

public $orderid;

	/**

	 * @return array action filters

	 */

	public function filters()

	{

		return array(

			'accessControl', // perform access control for CRUD operations

		);

	}


	/**

	 * Specifies the access control rules.

	 * This method is used by the 'accessControl' filter.

	 * @return array access control rules

	 */

	public function accessRules()

	{

		return array(

			array('allow',  // allow all users to perform 'index' and 'view' actions

				'actions'=>array('index','view'),

				'users'=>array('*'),

			),

			array('allow', // allow authenticated user to perform 'create' and 'update' actions

				'actions'=>array('create','update'),

				'users'=>array('*'),

			),

			array('allow', // allow admin user to perform 'admin' and 'delete' actions

				'actions'=>array('admin','delete'),

				'users'=>array('*'),

			),

			array('deny',  // deny all users

				'users'=>array('*'),

			),

		);

	}


	/**

	 * Displays a particular model.

	 * @param integer $id the ID of the model to be displayed

	 */

	public function actionView($id)

	{

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

			'model'=>$this->loadModel($id),

		));

	}


	/**

	 * Creates a new model.

	 * If creation is successful, the browser will be redirected to the 'view' page.

	 */


	public function actionCreate()

	{

		$model=new Order;

		$orderdetail=new OrderDetail();

		$product=new Products();

		

		$date=date('y-m-d');


		// Uncomment the following line if AJAX validation is needed

		//$this->performAjaxValidation(array($model,$orderdetail));


		

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

		{	

			

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

				$model->lat='12.53';

				$model->lng='13.2';

				$model->completed_date=$date;

				$model->received_date=$date;

				$model->created_date=$date;

				$model->upated_date=$date;

				

				

				

			if($model->save())

				

			if(isset($_POST['OrderDetail'])){

				$orderdetail->attributes=$_POST['OrderDetail'];

				//$lastinput=$_POST['lastInput'];

				//$qty=$orderdetail->qty=$_POST['qty'];

				//$productid=$orderdetail->product_id=$_POST['product_id'];

				

				for($i=1;$i<=count($_POST['product_id']);$i++){

					$orderdetail->order_id= $model->order_id;

					$orderdetail->order_item_status=1;

					$orderdetail->created_date=$date;

					$orderdetail->updated_date=$date;

			

					$orderdetail->product_id=$_POST['product_id'][$i];

					$orderdetail->qty=$_POST['qty'][$i];

					

					$orderdetail->save();

					

					}

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

			}

				//$OrderDetailController=new OrderDetail();

				//$this->redirect(array('OrderDetail/create'));

		}

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

			'model'=>$model,

			'orderdetail'=>$orderdetail,

			'product'=>$product,

			

		));

	}

	




	

/*

public function actionCreate()

{

    Yii::import('ext.multimodelform.MultiModelForm');

 

    $model = new Order;

 

    $orderdetail=new OrderDetail();

	$product=new Products();

	

    $validatedMembers = array();  //ensure an empty array

 

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

    {

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

 

        if( //validate detail before saving the master

            MultiModelForm::validate($model,$validatedMembers,$deleteItems) &&

            $model->save()

           )

           {

             //the value for the foreign key 'groupid'

             $masterValues = array ('order_id'=>$model->order_id);

             if (MultiModelForm::save($model,$validatedMembers,$deleteMembers,$masterValues))

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

            }

    }

 

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

        'model'=>$model,

        //submit the member and validatedItems to the widget in the edit form

        'orderdetail'=>$orderdetail,

		'product'=>$product,

        'validatedMembers' => $validatedMembers,

    ));

}

*/

	

	/**

	 * Updates a particular model.

	 * If update is successful, the browser will be redirected to the 'view' page.

	 * @param integer $id the ID of the model to be updated

	 */

	public function actionUpdate($id)

	{

		$date=date('y-m-d');

		$model=$this->loadModel($id);

		//$orderdetail=new OrderDetail();

		

		$product=new Products();

		$orderdetail=OrderDetail::model()->findByAttributes(array('order_id'=>$_GET['id']));

		//$product->findByAttributes(array('order_id'=>$_GET['id']));

		

		// Uncomment the following line if AJAX validation is needed

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


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

		{

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

			

		

			$model->lat='12.53';

			$model->lng='13.2';

			$model->completed_date=$date;

			$model->received_date=$date;

			$model->created_date=$date;

			$model->upated_date=$date;

			

			

			

			if($model->save())

				if(isset($_POST['OrderDetail'])){

			$orderdetail->attributes=$_POST['OrderDetail'];

			

			$orderdetail->order_id= $model->order_id;

			$orderdetail->order_item_status=1;

			$orderdetail->created_date=$date;

			$orderdetail->updated_date=$date;

		

			if($orderdetail->save())

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

			}

				

		}


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

			'model'=>$model,

			'orderdetail'=>$orderdetail,

			'product'=>$product,

			

		));

	}


	/**

	 * Deletes a particular model.

	 * If deletion is successful, the browser will be redirected to the 'index' page.

	 * @param integer $id the ID of the model to be deleted

	 */

	public function actionDelete($id)

	{

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

		{

			// we only allow deletion via POST request

			$this->loadModel($id)->delete();


			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser

			if(!isset($_GET['ajax']))

				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));

		}

		else

			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');

	}


	/**

	 * Lists all models.

	 */

	public function actionIndex()

	{

		$dataProvider=new CActiveDataProvider('Order');

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

			'dataProvider'=>$dataProvider,

		));

	}


	/**

	 * Manages all models.

	 */

	public function actionAdmin()

	{

		$model=new Order('search');

		$model->unsetAttributes();  // clear any default values

		

		if(isset($_GET['Order']))

			$model->attributes=$_GET['Order'];


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

			'model'=>$model,

		));

	}


	/**

	 * Returns the data model based on the primary key given in the GET variable.

	 * If the data model is not found, an HTTP exception will be raised.

	 * @param integer the ID of the model to be loaded

	 */

	public function loadModel($id)

	{

		$model=Order::model()->findByPk((int)$id);

		if($model===null)

			throw new CHttpException(404,'The requested page does not exist.');

	return $model;

		

	//$model=Order::model()->with(Products::model(),OrderDetail::model())->findByPk((int)$id);

  // if($model===null)

   //     throw new CHttpException(404,'Page not found.');

    //return $model;

		

	}


	/**

	 * Performs the AJAX validation.

	 * @param CModel the model to be validated

	 */

	protected function performAjaxValidation($model)

	{

		if(isset($_POST['ajax']) && $_POST['ajax']==='order-form')

		{

			echo CActiveForm::validate($model);

			Yii::app()->end();

		}

	}

}