0

I am trying a sample application in yii.. I faced a problem when I created new view page name with cart.php. In this page I am trying to access model variable.Where it is saying undefined variable model..

View page

<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>
3
  • 1
    add your controller code also. Commented Jan 7, 2014 at 8:00
  • in your controller, $this -> render('cart',array('model' => $model)); where $model is the Model for the database table you are using or Class you are using. Commented Jan 7, 2014 at 8:03
  • Are you passing model to view page? Commented Jan 7, 2014 at 8:03

3 Answers 3

2

In your controler

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

    $this->render('YourPageViewFile', array('model'=>$model));
}
Sign up to request clarification or add additional context in comments.

Comments

1

we need to add the code below to the action method where we redirect to ui page this will auto load the model to ui in yii framework

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

Comments

0

Use Below code you controller you have primary key "$id"

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.