CakePHP:为什么我不能用set()或save()更新记录?

CakePHP:为什么我不能用set()或save()更新记录?,php,cakephp,cakephp-2.0,cakephp-2.1,Php,Cakephp,Cakephp 2.0,Cakephp 2.1,我整晚都在试图更新这样的记录: $r = $this->Question->read(NULL, $question['Question']['id']); debug($r);// This is a complete Question array $this->Question->set('status', 'version'); $s =

我整晚都在试图更新这样的记录:

                $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;
$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);
这两条评论显示了我尝试过的变体,但都没有效果

@迪佩什:

                $this->data = $this->Question->read(NULL, $question['Question']['id']);
                $this->Question->status = 'version';
                $s = $this->Question->save($this->data);
                debug($s);
                exit;
@迪佩什二世:

                $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;
@迪佩什三世:
(已删除)

使用
$this->data
而不是
$r

示例

$this->data = $this->Question->read(NULL, $question['Question']['id']);
$this->set
用于设置变量值并将其传递给view,以便view可以访问它,其中,
$this->data
表示要存储在数据库中的数据

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

,这不是很“自动”,但我可以这样工作:

                $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;
$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);
基本上我只是手动构建数据数组。如果有人知道为什么这样做而不是我以前所做的,我很想听听。

试试这些台词

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


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
$dataFormModel = array('ModelName'=>array('col_name'=>$colValue));
$this->{ModelName}->set($dataForModel);
关于模型::集() 此方法用于在模型上设置数据,将要传递的数组的格式必须与model::save()方法中使用的格式相同,即如下所示

$data = $this->{ModelName}->find('first');
$this->set('dataForView',$data) // now you can access $data in your view as $dataForView
$dataFormModel = array('ModelName'=>array('col_name'=>$colValue));
$this->{ModelName}->set($dataForModel);
Model::set()
将仅接受此格式的参数,一旦成功设置,您可以执行以下操作

  • 直接按照模型中指定的验证规则验证此数据,如下所示

    $data = $this->{ModelName}->find('first');
    $this->set('dataForView',$data) // now you can access $data in your view as $dataForView
    
    $dataFormModel = array('ModelName'=>array('col_name'=>$colValue));
    $this->{ModelName}->set($dataForModel);
    
    $isValid=$this->ModelName->validate()

  • 通过调用
    Model::save()


  • 这是我需要对代码进行的唯一更改吗?或者我也要保存($this->data)
    ?@Emerson replace
    $this->Question->status='version'
    $this->data['Question']['status']='version'
    它仍然返回false,但也有以下警告:
    注意(8):间接修改重载属性QuestionsController::$data无效[APP/Controller/QuestionsController.php,第151行]
    @Emerson ohhh如果您使用的是cakephp2,那么请使用
    $this->request->data
    没有理由使用
    $this->request->data
    而不是局部变量。如果要将数据传播到编辑表单,则只能使用
    $this->request->data
    。但是,当在同一个模型上使用多个
    save()
    调用时,始终使用重置模型并准备执行下一个
    save()
    是否设置了验证?如果是这样,您能在
    debug($s)
    之后立即执行
    debug($this->Question->validationErrors)
    吗?这是个好主意。我不知道那种方法。但你看到我下面的答案了吗?验证不也适用于我的解决方案吗?是的,但我猜您的解决方案是有效的,因为通过手动添加所有数据,您可以正确验证,从而保存问题。在你的问题中发布你的模型,让我们看看我们得到了什么:)谢谢。对于Model::set(),我们如何告诉Cake要更新哪个记录?对于
    WHERE…
    ,它是否需要另一个参数?只需像这样设置模型concernd的id$this->model->id=$intVarSo首先运行该id()方法?在set()和save()之前?id不是方法,而是模型的class属性