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

时间:2014-02-27 05:36:02

标签: yii textfield preloading

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

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

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

在您的代码中,您错过了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 ); ?>