2014-01-14 24 views
-2

我有麻煩在CakePHP中的控制器創建方法表中的更新我現有的行,任何人都可以建議我適當的模型方法在表更新的行該模型的方法來更新該行中的CakePHP

<?php 
class UsersController extends AppController 
{ 
public function update($id) 
    { 
     if(isset($_REQUEST['update'])) 
     { 
     // method of model to update the row  
     } 
    else 
    $this->set('user',$this->User->find('first',array('conditions'=>array('id'=>$id)))); 
    } 
} 
?> 
讀哪一個環節由您提供的教程後
+3

閱讀文檔有幫助。 http://book.cakephp.org/2.0/en/models/saving-your-data.html做博客教程... – burzum

回答

0
Try the code....... 

<?php 

class UsersController extends AppController { 

    public function update($id = null) { 
     if ($id) { 
      if ($this->request->is('post')) { 
       $this->User->id = $id; 
       $this->User->save($this->request->data); 
      } else { 
       $this->set('user', $this->User->find('first', array('conditions' => array('id' => $id)))); 
      } 
     } 
    } 

} 
?> 
0

@burzum我找到了解決我的問題,在模型updateAll()在模型中可用的方法使用這個我有更新表的行。

public function update($id) 
    { 
     if(isset($_REQUEST['update'])) 
     { 
      $this->User->id=$id; 
      if($this->User->updateAll(array('User.fname'=>"'".$_REQUEST['fname']."'",'User.lname'=>"'".$_REQUEST['lname']."'",'User.email'=>"'".$_REQUEST['email']."'"),array('id'=>$id))) 
       echo '<script>alert("update successfully")</script>'; 
      else 
       echo '<script>alert("failes to update ")</script>'; 
     } 
     else 
     $this->set('user',$this->User->find('first',array('conditions'=>array('id'=>$id)))); 
    }