4

我正在使用 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', $expenseClaim['ExpenseClaim']['id'], 'admin' => true), array('class' => 'btn btn-danger'), __('Are you sure you want to Approve # %s?',$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');
        }
    } 
}
4

1 回答 1

3

是的,这当然是可能的。

只是发布到类似的东西

/expenseclaims/approve/id

触发approve动作,例如:

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

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

于 2012-08-21T11:51:39.437 回答