2012-12-21 158 views
0

我希望能夠刪除用戶,但是當我嘗試時會引發以下錯誤。 「錯誤:在此服務器上未找到請求的地址'/ cakephp/users/delete/8?url = users%2Fdelete%2F8'。」刪除用戶無法正常工作

我刪除功能的代碼是

public function delete($id = null) 
{ 
     debug($this->User->delete()); 
     if (!$this->request->is('get')) 
     { 
      throw new MethodNotAllowedException(); 
     } 
     if ($this->User->delete($id)) 
     { 
      $this->Session->setFlash('The user with id: ' . $id . ' has been deleted.'); 
      $this->redirect(array('action' => 'usermanage')); 
     } 
    } 

[NEW] 我擺脫了usermanagement代碼和索引碼,以騰出空間給我的用戶模式代碼..

<?php 
App::uses('AuthComponent', 'Controller/Component'); 
class User extends AppModel { 

    public $primary_key = 'userID'; 
    public $validate = array(
     'username' => array(
      'required' => array(
       'rule' => array('notEmpty'), 
       'message' => 'A username is required' 
      ) 
     ), 

     'password' => array(
      'required' => array(
       'rule' => array('notEmpty'), 
       'message' => 'A password is required' 
      ) 
     ), 
     'name' => array(
      'required' => array(
       'rule' => array('notEmpty'), 
       'message' => 'Your name is required' 
      ) 
     ), 
     'surname' => array(
      'required' => array(
       'rule' => array('notEmpty'), 
       'message' => 'Your surname is required' 
      ) 
     ), 
     'memberType' => array(
      'valid' => array(
       'rule' => array('inList', array('admin', 'responder', 'volunteer')), 
       'message' => 'Please enter a valid role', 
       'allowEmpty' => false 
      ) 
     ), 
     'address1' => array(
      'required' => array(
       'rule' => array('notEmpty'), 
       'message' => 'Please enter your first line of address' 
      ) 
     ), 
     'address2' => array(
      'required' => array(
       'rule' => array('notEmpty'), 
       'message' => 'please enter your second line of address' 
      ) 
     ), 
     'town' => array(
      'required' => array(
       'rule' => array('notEmpty'), 
       'message' => 'Please enter your town' 
      ) 
     ), 
     'postCode' => array(
      'required' => array(
       'rule' => array('notEmpty'), 
       'message' => 'please enter your postcode' 
      ) 
     ), 
     'dob' => array(
      'required' => array(
       'rule' => array('notEmpty'), 
       'message' => 'please enter your date of birth' 
      ) 
     ), 
     'emailAddress' => array(
      'required' => array(
       'rule' => array('notEmpty'), 
       'message' => 'please enter your email address' 
      ) 
     ), 
     'phoneNumber' => array(
      'required' => array(
       'rule' => array('notEmpty'), 
       'message' => 'please enter your phone number' 
      ) 
     ), 
     'mobileNumber' => array(
      'required' => array(
       'rule' => array('notEmpty'), 
       'message' => 'please enter your mobile number' 
      ) 
     ) 
    ); 

    public function beforeSave($options = array()) { 
     if (isset($this->data[$this->alias]['password'])) { 
      $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']); 
     } 
    } 
} 
?> 

任何幫助將不勝感激!

+1

請添加相關路線到您的文章。其他操作是否像編輯和視圖一樣工作? –

+0

編輯功能也不起作用,我爲那個創建了一個問題,但只有不成功的答案:(我現在添加了索引頁面(這導致用戶到用戶管理頁面) – iwj145

+0

'/ cakephp'你文件根目錄? –

回答

2

這個錯誤可以通過兩種方式(皮卡之一)進行修正:

1 - 改變你的行動上刪除對:

if (!$this->request->is('post') && !$this->request->is('put')) { 
    throw new MethodNotAllowedException(); 
} 

2 - 不使用postLink,並更改刪除操作:

<?php echo $this->Html->link('Delete', array('controller' => 'users', 'action' => 'delete', $user['User']['userID'])); ?> 

public function delete($id = null) { 
     $this->User->id = $id; 

     if(!$this->User->exists()) { 
      throw new NotFoundException(__('Invalid user')); 
     } 

     if ($this->User->delete($id)) { 
      $this->Session->setFlash('The user with id: ' . $id . ' has been deleted.'); 
      $this->redirect(array('action' => 'usermanage')); 
     } 
} 

[編輯]
爲了您的PrimaryKey問題:
在用戶模式:

public $primary_key = 'userID'; 

在其它型號(屬於關聯,的hasMany,hasBelongsToMany):

public $belongsTo = array(
    'User' => array(
     'className' => 'User', 
     'foreignKey' => 'userID' 
    ) 
); 

對不起,我的英語,我是巴西人。

+0

hello Patrick,謝謝你的回答,我認爲我已經取得了一些進展...但我現在留下的唯一問題是sql語句似乎認爲我的用戶ID被稱爲'id'而不是' userID',我該如何解決這個問題? – iwj145

+0

您需要爲該模型設置正確的主鍵。 [見文檔](http://book.cakephp.org/2.0/en/models/model-attributes.html#model-primarykey) – noslone

+0

@ user1876222看看我的答案,我編輯了它的結尾。 –