gpt4 book ai didi

Cakephp 表单助手 PostLink 编辑

转载 作者:行者123 更新时间:2023-12-01 02:32:48 24 4
gpt4 key购买 nike

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

Controller 代码:
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

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

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

关于Cakephp 表单助手 PostLink 编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12053984/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com