2014-01-14 31 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)))); 
    }