0

Cakephp1 という名前のデータベースに、tasks という名前のテーブルがあります。コントローラーに次のコードを記述しました。

 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が初めてで、これは私が試している私の例です。私を助けてください。列を更新するにはどうすればよいですか?

4

1 に答える 1

0

レコードを更新するには、保存機能を使用し、更新するレコード$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.');   
        } 
    } 
 }

また、このドキュメントを読んで、保存 (挿入、更新) する方法を理解する必要があります。

于 2013-08-22T07:25:36.783 に答える