CakePHP:为什么我无法使用set()或save()更新记录?

3

我已经花了一整晚的时间来更新这样的记录:

                $r = $this->Question->read(NULL, $question['Question']['id']);
                debug($r);// This is a complete Question array
                $this->Question->set('status', 'version');
                $s = $this->Question->save();
                //$s = $this->Question->save($r['Question']);//this also doesn't work
                debug($s); // = False every time!! Why??
                exit;

这两条评论显示了我尝试过但都没有起作用的变化。
@ Dipesh:
                $this->data = $this->Question->read(NULL, $question['Question']['id']);
                $this->Question->status = 'version';
                $s = $this->Question->save($this->data);
                debug($s);
                exit;

@Dipesh II:

                $this->request->data = $this->Question->read(NULL, $question['Question']['id']);
                debug($this->data);
                //$this->Question->status = 'version';
                $this->request->data['Question']['status'] = 'version';
                $s = $this->Question->save($this->request->data);
                //$s = $this->Question->save($r['Question']);
                debug($s);
                exit;

@ Dipesh III: (已删除)


你是否为问题设置了验证?如果是这样的话,在debug($s)之后可以执行debug($this->Question->validationErrors)吗? - Nunser
这是个好主意。我之前不知道有这个方法。但你看到我下面的回答了吗?验证难道不也适用于我的解决方案吗? - emersonthis
是的,但我猜测你的解决方案之所以有效,是因为通过手动添加所有数据,你可能正在正确验证并因此保存问题。在你的问题中发布你的模型,让我们看看我们得到了什么 :) - Nunser
4个回答

1
CakePHP在Models::set()Controller::set();中都提供了一个名为set()的方法。
关于Controller::set(): 该方法用于从任何控制器方法设置视图级别变量。例如,从模型获取记录并将其设置为视图以向客户端显示,如下所示:
$data = $this->{ModelName}->find('first');
$this->set('dataForView',$data) // now you can access $data in your view as $dataForView

关于Model::set() 这个方法用于在模型上设置数据,传递的数组格式必须与Model::save()方法中使用的相同,即像这样。
$dataFormModel = array('ModelName'=>array('col_name'=>$colValue));
$this->{ModelName}->set($dataForModel);

Model::set() 只接受这种格式的参数。一旦成功设置,可以执行以下操作:

  1. 直接在模型中通过指定的验证规则验证此数据,如下所示

    $isValid = $this->ModelName->validate();

  2. 通过调用 Model::save() 保存/更新数据


谢谢。对于Model::set(),我们如何告诉Cake要更新哪条记录?它是否需要另一个WHERE...参数? - emersonthis
只需像这样设置相关模型的ID,例如 $this->Model->id = $intVar。 - Sumit Kumar
那么先运行id()方法?在set()和save()之前? - emersonthis
id 不是方法,而是模型的类属性。 - Sumit Kumar

0

使用$this->data代替$r

示例

$this->data = $this->Question->read(NULL, $question['Question']['id']);

$this->set 用于设置变量的值并将其传递给视图,以便视图可以在需要时访问它,而 $this->data 则表示要存储在数据库中的数据。

如果您使用的是 Cake 2.0,则应将只读的 $this->data 替换为 $this->request->data


这是我需要在代码中做的唯一更改吗?还是我也需要 save($this->data) - emersonthis
@Emerson 请将 $this->Question->status = 'version'; 替换为 $this->data['Question']['status'] = 'version'; - Dipesh Parmar
它仍然返回false,但同时也出现了此警告:注意(8):过载属性QuestionsController :: $ data的间接修改无效[APP / Controller / QuestionsController.php,第151行] - emersonthis
@Emerson 噢,你正在使用CakePHP2,那就使用$this->request->data - Dipesh Parmar
1
没有理由使用$this->request->data而不是本地变量。只有在想要将数据传播到编辑表单时,才会使用$this->request->data然而,当在同一模型上使用多个save()调用时,始终使用Model::create()来重置模型并准备执行下一个save() - thaJeztah
显示剩余5条评论

0

虽然不是非常“自动化”,但我能够像这样使其工作:

$set_perm_id = 42;//who cares
$data = array(
            'Question'=> array(
                        'id'=> $question['Question']['id'],
                        'perm_id'=> $set_perm_id,
                        'status'=>'version'
                        )
            );
$s=$this->Question->save($data);

基本上我只是手动构建数据数组。如果有人知道为什么这样做可以取代我之前的方法,我很乐意听听。


0

试试这些代码行...

$this->Question->id =  $question['Question']['id'];
$this->Question->set('status','version');
$this->Question->save();

或者

$aUpdate["id"] = $question['Question']['id'];
$aUpdate["status"] = "version";
$this->Question->save($aUpdate);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接