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'));
        }
  }

[新] 我摆脱了用户管理代码和索引代码,为我的用户模型代码腾出空间..

<?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']);
        }
    }
}
?>

任何帮助将不胜感激!

4

1 回答 1

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';

在其他模型中(belongsTo、hasMany、hasBelongsToMany):

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

对不起我的英语,我是巴西人。

于 2012-12-21T10:57:03.513 回答