【问题标题】:Update form Cakephp 3更新表格 Cakephp 3
【发布时间】:2015-04-10 08:45:57
【问题描述】:

我目前正在使用 cakephp 3 开发一个项目。

我有一个表单来添加在我的控制器中使用它的客户端:

    public function add(){

        $clients = $this->Clients->newEntity();
        if($this->request->is('post')){
            $clients = $this->Clients->patchEntity($clients, $this->request->data);
            if($this->Clients->save($clients)){
                $this->Flash->success(__('Client has been created.'));
                return $this->redirect(['controller'=>'Clients','action'=>'index']);
            }
            $this->Flash->error(__('Client hasnt been created.'));
        }
        $this->set('clients',$clients);

    }

然后我想有可能修改我的一个客户。 我有一个客户表,当我点击它们时,我有一个修改按钮(jQuery)。 然后我在我的修改页面上。我对 cake 中的文档做了一些测试,但似乎我不明白它是如何工作的以及我应该使用什么工具。

目前,我的控制器上有这个:

public function modify($id = null){
            if(empty($id)){
                throw new NotFoundException;
            }
            $clients = $this->Clients->get($id);
            /* there should be the modify code */
            $this->set('clients', $clients);

        }

如我所说,我真的不知道该使用什么...有什么帮助吗?

【问题讨论】:

  • 通过提供 id 尝试 updateAll。
  • 感谢您的回答@anantkumarsingh。我正在寻找 updateAll(),但我没有得到我应该把它放在 $fields 和 $conditions 中的内容。我想要的 SQL 请求是 UPDATE clients SET(表单中的所有字段) WHERE id = $id...
  • 请尝试告诉我它是否有效?
  • 我编辑了我的答案,我想它可以与 updateAll 一起使用,但是来自 cake 的文档有点无信息

标签: php forms cakephp cakephp-3.0


【解决方案1】:

编辑记录的代码非常简单:

public function modify($id = null){
        if(empty($id)){
            throw new NotFoundException;
        }
        $client = $this->Clients->get($id);
        if ($this->request->is(['post', 'put']) {
            $client = $this->Clients->patchEntity($client, $this->request->data);
            if ($this->Clients->save($client)) {
                return $this->redirect($someURL);
            }
        }
        $this->set('client', $client);

}

【讨论】:

  • 感谢@JoséLorenzo,它有效!我猜在这种情况下 updateAll 没用!
  • 更多关于修补实体(仅更新已更改的字段)的信息可以在此处的文档中找到:book.cakephp.org/3.0/en/orm/…
猜你喜欢
  • 1970-01-01
  • 2016-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多