Yii从另一个模型预加载表单文本字段

Yii从另一个模型预加载表单文本字段,yii,textfield,preloading,Yii,Textfield,Preloading,我正在尝试从另一个模型中预加载文本字段,我使用了下面的代码不起作用,这是我的代码 public function actionCreate($id) { $id1 = Yii::app()->user->id; $model = new DoctorLists; $prof = new Profile; $prof = Profile::model()->findByPk($id)

我正在尝试从另一个模型中预加载文本字段,我使用了下面的代码不起作用,这是我的代码

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

在代码中,您遗漏了
if
语句的大括号。试试下面的代码

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.');
 } 
将视图代码更改为:

<?php echo Chtml::textField('doctor_firstname', $model->doctor_firstname ); ?>

我在查询中做了一个简单的更改,并使其工作,下面是工作代码 我可以从另一个模型中预加载内容

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

当然是这样,但它仍然不起作用。我认为这个问题是关键所在,我已经对上面的代码进行了相应的编辑,谢谢你修复了它。请添加您的答案作为答案,而不是提供覆盖问题的答案。我们鼓励您这样做,您也可以将其勾选为已解决。你会在这里这样做吗?然后,您可以使用编辑工具将问题回滚到其先前的状态。