Cakephp Form helper PostLink编辑

时间:2012-08-21 11:36:47

标签: cakephp

我正在使用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');
        }
    } 
}

1 个答案:

答案 0 :(得分:3)

是的,当然这是可能的。

发布到

之类的内容
/expenseclaims/approve/id

触发approve操作,例如:

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

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