Search code examples
yiitextfieldpreloading

Yii Pre-loading form text fields from another model


I am trying to pre-load the text fields from another model, I used the following code not working, here is my code.

public function actionCreate($id)
{                
    $id1 = Yii::app()->user->id;            
    $model = new DoctorLists;
    $prof = new Profile;
    $prof = Profile::model()->findByPk($id)

    $model->doctor_firstname = $prof->firstname;
    $model->doctor_lastname = $prof->lastname;
    $model->doctor_email = $prof->email;
    $model->doctor_mobile_no = $prof->mobile_num;
    $model->consult_no = $prof->alter_mobile_num; 

    if(isset($_POST['DoctorLists'])
    { 
        $model->attributes=$_POST['DoctorLists'];    
        if($model->save())
        {                           
            $this->redirect(array('view','id'=>$model->doctor_id));
        }
    }
    $this->render('create',array(
                                'model'=>$model,
                  ));
}

Solution

  • I made a simple change in the query and got it to work and here is the working code Now I could preload the contents from another model

    public function actionCreate($id)
    {                
    $id1 = Yii::app()->user->id;            
    $model = new DoctorLists;
    $prof = new Profile;
    $prof = Profile::model()->find('user_id=:user_id', array(':user_id'=>$id)); 
    
    $model->doctor_firstname = $prof->firstname;
    $model->doctor_lastname = $prof->lastname;
    $model->doctor_email = $prof->email;
    $model->doctor_mobile_no = $prof->mobile_num;
    $model->consult_no = $prof->alter_mobile_num; 
    
    if(isset($_POST['DoctorLists'])
    { 
        $model->attributes=$_POST['DoctorLists'];    
        if($model->save())
        {                           
            $this->redirect(array('view','id'=>$model->doctor_id));
        }
    }
    $this->render('create',array(
                                'model'=>$model,
                  ));
    }