yii控制器无法设置帖子

时间:2012-02-08 02:59:18

标签: yii

朋友们!

我生成了一个动态产品字段和数量与javascript一起归档,以便客户对一个产品进行订购。但为什么控制器无法设置POST ['OrderDetail']?控制器只能保存一个型号,订单。 请帮助纠正我。

我有四个模型:Product,Customer,Order和Order_detail。

查看/顺序/ _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('divProduct');
  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---','name'=>'productorder[]')); ?>+<?php echo $form->textField($orderdetail,'qty', array('name'=>'qtyorder[]')); ?>+'<a href="#" onclick="removePhoto('+divIdName+')">Remove</a>';

  ni.appendChild(newdiv);

}
 function removePhoto(divNum) {

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

      var olddiv = document.getElementById(divNum);
      d.removeChild(olddiv);
        var minus=document.getElementById('countLastInput').value;
        document.getElementById('countLastInput').value=minus-1;
    }

</script>

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

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

<?php /*?>
<div class="row">
<?php echo $form->labelEx($orderdetail,'product_id'); ?>
<?php echo $form->dropDownList($orderdetail,'product_id',CHtml::listData(Products::model()->findAll(),'product_id','product_name'),
array('empty' => '--- Choose---')); ?>
<?php echo $form->error($orderdetail,'product_id'); ?>
</div>

 <div class="row">
<?php echo $form->labelEx($orderdetail,'qty'); ?>
<?php echo $form->textField($orderdetail,'qty'); ?>
<?php echo $form->error($orderdetail,'qty'); ?>
</div>
<?php /*?>

<?php //$orderdetail=new OrderDetail();?>

<?php //echo $this->renderPartial("_partial_order",array('orderdetail'=>$orderdetail));?>

<?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 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','showlog'),
                '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 actionShowLog(){
    //Yii::log("hi, h r u?" ,"trace","application.controllers.OrderController");
    var_dump(Yii::getLogger()->getLogs());
    //Yii::trace("The actionCreate() method is being requested","application.controllers.OrderController");

}
    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())
            {

                Yii::log("order save","info","application.controllers.OrderController");

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

                for($i=1;$i<count($_POST['productorder']);$i++){
                //$orderdetail->attributes=$_POST['OrderDetail'];
                $orderdetail->product_id=$_POST['productorder'][$i];
                $orderdetail->qty=$_POST['qtyorder'][$i];
                    $orderdetail->order_id= $model->order_id;
                    $orderdetail->order_item_status=1;
                    $orderdetail->created_date=$date;
                    $orderdetail->updated_date=$date;

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



                //$qty=$orderdetail->qty=$_POST['qty'];
                //$productid=$orderdetail->product_id=$_POST['txtproduct'];
            /*  
                for($i=1;$i<=count($qty);$i++){
                    $orderdetail->order_id= $model->order_id;
                    $orderdetail->order_item_status=1;
                    $orderdetail->created_date=$date;
                    $orderdetail->updated_date=$date;

                    $orderdetail->product_id=1;
                    //$orderdetail->qty=$qty[$i];

                    //$orderdetail->save();
                    var_dump($_POST['qty']);
                    }
                    $this->redirect(array('view','id'=>$model->order_id));
            */
            }else{
            Yii::log("Failed ordedetails","warning","application.controllers.OrderController");
            }
                //$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();
        }
    }
}

提前致谢。

1 个答案:

答案 0 :(得分:1)

尝试启用CWebLogRoute以查看是否存在任何安全问题。

Yii wiki:How to log and debug variables using CWebLogRoute