无法使用 yii 框架编辑特定用户数据


Cannot edit particular user data using yii framework

我是 yii 框架的新手。我正在使用 yii 框架进行更新操作。我有名称为站点控制器.php的控制器,模型求职者配置文件.php,查看个人.php。我的表job_seeker_profile字段 id,user_id,contact_no,性别,dob,mstatus,地址。我无法通过表单发布来更新数据。

我的控制器是站点控制器.php

class SiteController extends Controller
{
    public function actionpersonal()
    {
        $user_id = trim($_GET['id']);
        $model = new jobseekerprofile();
        $model = jobseekerprofile::model()->find(array(
            'select' => 'contact_no,address', "condition" => "user_id=$user_id",
            'limit' => 1,));
        $model = $this->loadModel($user_id);
        if (isset($_POST['jobseekerprofile'])) {
            $model->attributes = $_POST['jobseekerprofile'];
            if ($model->save()) {
                $this->redirect(array('profile', 'user_id' => $model->user_id));
            }
        }
        $this->render('personal', array('model' => $model));
    }
    public function loadModel($user_id)
    {
        echo $model = jobseekerprofile::model()->findByPk($user_id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }
}

查看个人.php

    <div class="form">
<?php $form = $this->beginWidget('CActiveForm', array(
    'id' => 'login-form',
    'enableClientValidation' => false,
    'htmlOptions' => array(),
    'clientOptions' => array(
        'validateOnSubmit' => true
    ),
)); ?>
<?php
foreach (Yii::app()->user->getFlashes() as $key => $message) {
    echo '<div class="flash-' . $key . '">' . $message . "</div>'n";
}
?>
    <p class="note">Fields with <span class="required">*</span> are required.</p>
    <div class="row">
        <?php echo $form->labelEx($model, 'Contact No'); ?>
        <?php echo $form->textField($model, 'contact_no'); ?>
        <?php echo $form->error($model, 'contact_no'); ?>
    </div>
    <div class="row">
        <?php echo $form->labelEx($model, 'Address'); ?>
        <?php echo $form->textField($model, 'address'); ?>
        <?php echo $form->error($model, 'address'); ?>
    </div>
    <div class="row buttons">
        <?php echo CHtml::submitButton('Save'); ?>
    </div>

<?php $this->endWidget(); ?>

模特-求职者简介.php

 rules in model

public function rules()
  {
    return array(
                array('contact_no,gender,dob,address,mstatus','required'),
    );
 }

有人帮我吗?

您需要使用大写字母。

Jobseekerprofile.php至少$model = new Jobseekerprofile();,我什至会称它为:JobseekerProfile.php $model = new JobseekerProfile();之类的。

示例控制器(未测试):

public function actionPersonal($id) {
    $model = Jobseekerprofile::model()->findByPk($id);
    if (isset($_POST['Jobseekerprofile'])) {
        $model->attributes = $_POST['Jobseekerprofile'];
        if ($model->save())
            $this->redirect(array('view', 'id' => $model->id));
    }
    $this->render('personal', array(
        'model' => $model,
    ));
}

你也可以使用 Yii 的 Gii 可能性来制作这些标准形式。它将根据您的数据库及其中的关系生成表单。