douguangxiang0363 2014-03-31 11:54
浏览 42

在yii中编辑特定记录

I have a view section in my project,and using CGridView to list all the data from table,also have an edit and delete option within the grid to edit and delete specific row.

I am stuck with the edit section.I am working on how to get a specific row data dispalyed in editjob.php,I have done a few things,but no use.My codes are as follows,

In my view job section using CgridView,

  'buttons' =>array('update'=>array(

     'label'=>'edit',
     'url'=>'Yii::app()->controller->createUrl("UpdateJob",array("id"=>$data["id"]))',

   ))

In Model UpdateJob:

public function edit()
{

 $criteria=new CDbCriteria;

 $criteria->find('id','Admin',true);

 return new CActiveDataProvider('viewjob', array(
    'criteria'=>$criteria,
   // 'sort'=>array(
       // 'defaultOrder'=>'key_skills ASC',
   // ),
));

in controller:

public function actionUpdateJob()
{ 

     if(isset($_GET['id']))
    {
        $id=$_GET['id'];
    }

     $model = new UpdateJob('edit');
     $params = array('model' => $model,'id' => $id  //passing the id like this
    );



    $this->render('update', $params);
}

And finaly in view written something like ,but showing error

   <div class="row">
        <?php echo $form->labelEx($model,'Company Name'); ?>
        <?php echo Chtml::textField('posted_by',UpdateJob::model()->FindByPk($model->id)->posted_by);  ?>
        <?php echo $form->error($model,'posted_by'); ?>
    </div>

am I on right track.

  • 写回答

1条回答 默认 最新

  • dsfdsf23423 2014-04-01 06:18
    关注

    Youre loading a fresh model instead of fetching an existing one. Replace this line:

    $model = new UpdateJob('edit');
    

    By this line:

    $model = UpdateJob::model()->findByPk($id);
    

    To save the data you do this:

    if(isset($_POST['UpdateJob'])) {
        $model->scenario='edit';
        $model->attributes=$_POST['UpdateJob'];
        if($model->save())
            $this->redirect(array('admin');
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示