2014-01-07 39 views
0

我正在嘗試yii中的示例應用程序。當我使用cart.php創建新的視圖頁面名稱時遇到問題。在該頁面中我試圖訪問模型variable.Where跟它不確定變量模型..未定義的變量:yii中的模型

查看頁面

<div class="form"> 
<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'login-form', 
    'enableClientValidation'=>true, 
    'clientOptions'=>array(
     'validateOnSubmit'=>true, 
), 
)); ?> 


    <div> 
    <?php echo $form->labelEx($model,'cartId:'); ?> 
    <?php echo $form->textField($model,'cartId',array('size'=>20,'maxlength'=>120)); ?> 
    <?php echo $form->error($model,'cartId'); ?> 
    </div> 



<?php $this->endWidget(); ?> 
</div> 
+1

也添加你的控制器代碼。 –

+0

在你的控制器中,$ this - > render('cart',array('model'=> $ model));其中$ model是您正在使用的數據庫表的Model或您正在使用的Class。 –

+0

您是否在傳遞模型以查看頁面? –

回答

2

在你CONTROLER

public function actionYourPageName(){ 
    $model = YourModel::model()->findbypk($id); 

    $this->render('YourPageViewFile', array('model'=>$model)); 
} 
1

我們需要添加下面的代碼到動作方法,我們重定向到UI頁面,這將自動加載模型到UI在yii框架

public function actionName(){ 
    //if u have id 
    $model = YourModel::model()->findbypk($id); 
      **or** 
    //if u don't have id to retrive record 
    $model = new YourModel(); 
      **or** 
    // loadModel is default methode in controller 
    'model'=>$this->loadModel($id), 

    $this->render('YourPageViewFile', array('model'=>$model)); 
} 
0

使用下面的代碼你的控制器你有主鍵「$ id」

Public function actionPageName($id=NULL){ 
     $model = if($id)?YourModel::model()->findbypk($id):new YourModel(); 
     $this->render('page-name', array('model'=>$model)); 
}