0

I am getting an error
Fatal error: Call to a member function hasErrors() on a non-object in F:\xampp\yii\framework\web\helpers\CHtml.php on line 2253
when trying to access the url http://localhost/happybox/index.php/site/cart/3. The code of my view file cart.php is:

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(); 
}
?>

The code of my controller function is

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));
    }
}

Can someone please point out the error in this code, Its very very important for me. Please help.

4

2 回答 2

0

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

于 2013-10-22T02:54:22.910 回答
0

当我在小部件中创建 hiddenField 时,我遇到了同样的错误。这样它就起作用了:

echo $form->hiddenField(
    **YourModelName::model()**,
    'users_id',
    array('value' => Yii::app()->user->id)
);
于 2014-08-25T13:26:34.533 回答