更新表格 Cakephp 3
Update form Cakephp 3
我目前正在使用 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);
}
然后我想修改我的一个客户端。
我有 table 个客户,当我点击其中一个时,我会看到一个修改按钮 (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);
}
正如我所说,我真的不知道该用什么...有什么帮助吗?
编辑记录的代码非常简单:
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);
}
我目前正在使用 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);
}
然后我想修改我的一个客户端。 我有 table 个客户,当我点击其中一个时,我会看到一个修改按钮 (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);
}
正如我所说,我真的不知道该用什么...有什么帮助吗?
编辑记录的代码非常简单:
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);
}