【问题标题】:Yii Pre-loading form text fields from another modelYii 从另一个模型中预加载表单文本字段
【发布时间】:2014-02-27 05:36:01
【问题描述】:

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

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

【问题讨论】:

  • 很高兴您修复了它。与其提供覆盖问题的答案,不如将您的答案添加为答案。我们鼓励这样做,您也可以将其标记为已解决。你会在这里做吗?然后,您可以使用编辑工具将问题回滚到之前的状态。

标签: yii textfield preloading


【解决方案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,
              ));
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 当然正确,但它仍然无法正常工作。我认为关键是问题
    • 这个问题一直存在,我已经相应地编辑了上面的代码,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多