加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

Cakephp Form helper PostLink编辑

发布时间:2020-12-13 16:46:51 所属栏目:PHP教程 来源:网络整理
导读:我正在使用cakephp 2. 是否可以使用表单助手postLink函数来更新记录?基本上我想要一个“批准”按钮,将记录的“已批准”字段更改为1. 我能找到的一切只与执行删除功能有关?文档也没有详细介绍. 任何帮助都会很棒,提前谢谢 我的代码: ?php echo $this-Form-
我正在使用cakephp 2.

是否可以使用表单助手postLink函数来更新记录?基本上我想要一个“批准”按钮,将记录的“已批准”字段更改为1.

我能找到的一切只与执行删除功能有关?文档也没有详细介绍.

任何帮助都会很棒,提前谢谢

我的代码:

<?php echo $this->Form->postLink(__('Approve'),array(
                                     'controller' => 'expenseclaims','action' => 'edit',$expenseClaim['ExpenseClaim']['id'],'approved' => '1','approved_by' => $adminUser,),array(
                                     'class' => 'btn btn-danger'
                                 ),__('Are you sure you want to Approve # %s?',$expenseClaim['ExpenseClaim']['id']
                                )); ?>

新代码:查看帖子链接:

<?php echo $this->Form->postLink(__('Approve'),array('action' => 'approve','admin' => true),array('class' => 'btn btn-danger'),$expenseClaim['ExpenseClaim']['id']));
                                ?>

控制器代码:

public function admin_approve($id = null) {

    debug($this->request);

    $this->ExpenseClaim->id = $id;
    if (!$this->request->is('post') && !$this->request->is('put')) {
        throw new MethodNotAllowedException();
    }

    if (!$this->ExpenseClaim->exists()) {
        throw new NotFoundException(__('Invalid expense claim'));
    }

    if ($this->request->is('post') || $this->request->is('put')) {

        $this->request->data['ExpenseClaim']['approved'] = '1';
        $this->request->data['ExpenseClaim']['approved_by'] = $this->Auth->user('id');

        if ($this->ExpenseClaim->save($this->request->data)) {
            $this->Session->setFlash('The expense claim has been Approved','flash_success');
            $this->redirect(array('action' => 'index','admin' => true));
        } else {
            $this->Session->setFlash('The expense claim could not be approved. Please,try again.','flash_failure');
        }
    } 
}

解决方法

是的,当然这是可能的.

只是张贴到类似的东西

/expenseclaims/approve/id

触发批准操作,例如:

public function approve($id = null) { 
    if (!$this->request->is('post') && !$this->request->is('put')) {
        throw new MethodNotAllowedException();
    }
    //validate/save
}

当然,你也可以使它更通用

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读