Yii框架表单提交按钮显示错误

时间:2013-10-21 22:07:34

标签: php yii yii-extensions

我在尝试访问网址Fatal error: Call to a member function hasErrors() on a non-object in F:\xampp\yii\framework\web\helpers\CHtml.php on line 2253时收到错误<http://localhost/happybox/index.php/site/cart/3。我的视图文件cart.php的代码是:

Hello, <?php echo ucwords(Yii::app()->user->name); ?>,<br/>
This is your cart<br/>
Click on Order to Place and Order.<br/>

<?php 
$Orders = Orders::model()->findAll(array(
    'select'=>'products_id',
    'condition'=>'users_id=:id && type=:type',
    'params'=>array(':id'=>Yii::app()->user->id, ':type'=>'cart'),
));

?>
<?php 
if($Orders){
    foreach ($Orders as $order) {
    echo '<br/>'.$order->products->title.'<br/>'.$order->products->description.'<br/>';

    }
}else{
    echo 'Your Cart is Empty';
}

?><br/><br/>

<?php 
if($Orders){
   $form=$this->beginWidget('CActiveForm', array('id'=>'order-form')); 
   echo $form->hiddenField($model,'users_id',array('value'=>Yii::app()->user->id)); 
   echo CHtml::submitButton('Confirm Your Order'); 
   $this->endWidget(); 
}
?>

我的控制器功能代码是

public function actionCart($id){
    if(Yii::app()->user->isGuest){
        $this->redirect('login');
    }else{
        $model=$this->loadModel($id);
        if(isset($_POST['Orders']))
        {        
        //do something
        }
        $this->render('cart', array('model'=>$model));
    }
}

有人可以指出这段代码中的错误,这对我来说非常重要。请帮忙。

2 个答案:

答案 0 :(得分:0)

$model = $model=$this->loadModel($id);在您的代码和var_dump $模型中修复此行以查看它返回的内容,检查它是否为null或者不是CActiveRecord

答案 1 :(得分:0)

当我在窗口小部件中创建hiddenField时,我遇到了同样的错误。这种方式有效:

echo $form->hiddenField(
    **YourModelName::model()**,
    'users_id',
    array('value' => Yii::app()->user->id)
);