Cakephp 2.3 I';I’我试图编辑一列,但没有更新列,而是向列中添加了一个新列
我的数据库中有一个名为tasks的表,名为cakephp1。 我在控制器中编写了以下代码:Cakephp 2.3 I';I’我试图编辑一列,但没有更新列,而是向列中添加了一个新列,cakephp-2.3,Cakephp 2.3,我的数据库中有一个名为tasks的表,名为cakephp1。 我在控制器中编写了以下代码: function edit($id = null) { if (!$id) { $this->Session->setFlash('Invalid Task'); $this->redirect(array('action'=>'index'), null, true); } if (empty($this->data
function edit($id = null) {
if (!$id) {
$this->Session->setFlash('Invalid Task');
$this->redirect(array('action'=>'index'), null, true);
}
if (empty($this->data)) {
$this->data = $this->Task->find(array('id' => $id));
} else {
if ($this->Task->updateAll(debug($this->data))){
$this->Session->setFlash('The Task has been saved');
$this->redirect(array('action'=>'index'), null, true);
} else {
$this->Session->setFlash('The Task could not be saved.
Please, try again.');
}
}
}
我是cakephp新手,这是我正在尝试的示例。请帮帮我。如何更新列?要更新记录,可以使用保存功能,并在
$this->request->data
中提供要更新的记录代码id。另外,$this->data
已被弃用,可能无法使用,您应该使用$this->request->data
例如,如果您的$this->request->data是:
array('id' => 10, 'title' => 'My new title');
在下面,id=10的记录的控制器代码标题将被更新
function edit($id = null) {
if (!$id) {
$this->Session->setFlash('Invalid Task');
$this->redirect(array('action'=>'index'), null, true);
}
if (empty($this->request->data)) {
$this->request->data = $this->Task->find(array('id' => $id));
} else {
if ($this->Task->save($this->request->data)){
$this->Session->setFlash('The Task has been saved');
$this->redirect(array('action'=>'index'), null, true);
} else {
$this->Session->setFlash('The Task could not be saved.
Please, try again.');
}
}
}
此外,您还应该阅读以了解如何保存(插入、更新)