3

I've been trying all night to update a record like this:

                $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;

The two comments show variations I've tried but didn't work either.

@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: (removed)

4

4 回答 4

1

cakePHP 提供了一个名为 set() 的Models::set()方法Controller::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()

于 2013-10-24T18:42:59.023 回答
0

使用$this->data代替$r

例子

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

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

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

于 2013-05-02T04:44:21.567 回答
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);

基本上我只是手动构建数据数组。如果有人知道为什么这有效,而不是我以前所做的,我很想听听。

于 2013-05-02T14:12:39.753 回答
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);
于 2013-05-03T04:30:44.280 回答