我正在尝试在cakephp 2中执行编辑操作,我正在为UsersController :: edit()和Undefined变量获取Missing参数1:id

时间:2016-01-20 06:54:20

标签: cakephp

这是我的UsersController文件,在其给定的编辑操作下面,没有传递id它工作正常,但是在下一个表单中,但是id变得可见。

public function edit($id){
$data=$this->User->findbyId($id);
if($this->request->is(array('post','put'))){
    $this->User->id=$id;                //id is passed from one form to another
    if($this->User->save($this->request->data)){
    $this->session->setFlash('Edited successfully');
    $this->redirect('index');
    $this->request->data=$data;
}
    }
}


 this is my edit.ctp flie

<html>
<body>
<h1>Edit a new user </h1>
<?php 
    echo $this->form->create('User');
    //echo $this->Form->input('id');
    echo $this->Form->input('first_name');
    echo $this->Form->input('last_name');
    echo $this->Form->input('email');
    echo $this->Form->end('save');
?>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

当URL看起来像.... / users / edit而不是.... / users / edit / 2时,你是否得到了缺少的参数错误?在这种情况下,请更改此

   public function edit($id) 

           to 

   public function edit($id = null)

此外,如果URL中没有传递id,它应该给我们一条错误消息,因为我们不确定我们需要编辑哪个用户。

试试这个,

           if(!$id){

               $this->Session->setFlash("User not found!");

               $this->redirect( $this->referer() );
           }

           $data   =   $this->User->findbyId($id);

           if(empty($data)){

               $this->Session->setFlash("User not found!");

               $this->redirect( $this->referer() );
           }

和平!的xD