Question

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,
                  ));
}
Was it helpful?

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

OTHER TIPS

In your code, you missed braces for if statement. Try below code

if($id==$pid)
 {
    $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; 
 }
 else
 {
    throw new CHttpException(404,'The requested page does not exist.');
 } 

Change your view code as:

<?php echo Chtml::textField('doctor_firstname', $model->doctor_firstname ); ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top