【问题标题】:Cannot edit perticular user's data using yii framework无法使用 yii 框架编辑特定用户的数据
【发布时间】:2014-04-01 07:39:08
【问题描述】:

我是 yii 框架的新手。我正在使用 yii 框架进行更新操作。我有名为 sitecontroller.php 的控制器,模型 jobseekerprofile.php,查看personal.php。我无法通过表单发布来更新数据。

我的控制器是 sitecontroller.php

<?php
class SiteController extends Controller
 {
 public function actionpersonal()
  {

    $user_id = trim($_GET['id']);
    $model = new jobseekerprofile();
    if(isset($_POST['jobseekerprofile']))
     {
        $model->attributes=$_POST['jobseekerprofile'];
        if($model->save())
           {
              $this->redirect(array('profile','user_id'=>$model->user_id));
           }   
    }

    $model=jobseekerprofile::model()->find(array(
                                         'select'=>'contact_no,address',"condition"=>"user_id=$user_id",
                                         'limit'=>1,));
    $this->render('personal',array('model' =>$model));
  }
}

?>

我的查看页面personal.php

<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'login-form',
'enableClientValidation'=>true,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
    '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->textField($model,'pp_status', array('value'=>'Open', 'readonly' => 'true')); ?>
    <?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(); ?>

我的模特jobseekerprofile.php

<?php

类 Jobseekerprofile 扩展 CActiveRecord { 公共 $contact_no; 公共$dob; 公共$性别; 公共 $mstatus; 公共$地址; 公共 $user_id;

public static function model($className=__CLASS__)
 {
return parent::model($className);
 }
public function tableName()
{
    return 'job_seeker_profile';
}
public function rules()
  {
    return array(
    array('contact_no,gender,dob,address,mstatus','required'),
    );
 }

} ?>

有人帮帮我吗?

【问题讨论】:

  • 在您的表单中添加操作并更改控制器函数名称 actionpPrsonal
  • 控制器名称-Sitecontroller.php,,我的动作名称-personal,,我的视图文件名-personal.php

标签: php yii


【解决方案1】:

因为您在每次请求时都会创建新的 jobseekerprofile 模型,所以此代码将在您每次提交时创建新的 jobseekerprofile 记录。将您的操作更改为:

public function actionpersonal()
{

$user_id = trim($_GET['id']);
$model=jobseekerprofile::model()->find(array(
                                     'select'=>'contact_no,address',"condition"=>"user_id=$user_id",
                                     'limit'=>1,));
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));
}

这应该可行。

【讨论】:

  • 尝试将 'enableClientValidation' 设置为 false,删除 'enctype',并检查 $jobseekerprofile->errors。求职者资料规则可能存在问题。
  • gender,dob,mstatus 字段为必填项,但未设置。在保存模型之前设置它。
【解决方案2】:

您尚未提供要编辑的用户的主键。 尝试关注

$model=jobseekerprofile::model()->findByPk($id); $id 是表的主键。 然后你可以写如下

if($model->save())
           {
              $this->redirect(array('profile','user_id'=>$model->user_id));
           }   

如果你只使用 $model->save() 它将插入数据,但不会更新。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    • 2015-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多