2

我正在尝试编辑我的用户信息,我所做的唯一更改是在下面的控制器函数中将 id 从 userID 更改为 id,因为我已经在数据库、模型和视图中更改了它。

Public function useredit() {
            $this->User->id = $this->Session->read('Auth.User.id');
            $this->request->data = $this->Session->read('Auth.User.id');
            if ($this->request->is('post')) {
                if ($this->User->save($this->request->data)) {
                    $this->Session->setFlash(__('The User has been saved', true));
                    $this->redirect(array('action' => 'index'));
                } else {
                    $this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
                }
            } else {
                $this->request->data = $this->User->read(null);
                isset($this->request->data['User']['password']);
            }
        }

但现在它告诉我Error: Table posts for model Post was not found in datasource default. 这是我的模型:

<?php

App::uses('AppModel', 'Model');
App::uses('AuthComponent', 'Controller/Component');

class User extends AppModel {

    public $belongsTo = array('Group');
    public $actsAs = array('Acl' => array('type' => 'requester'));

    public function parentNode() {
        if (!$this->id && empty($this->data)) {
            return null;
        }
        if (isset($this->data['User']['group_id'])) {
            $groupId = $this->data['User']['group_id'];
        } else {
            $groupId = $this->field('group_id');
        }
        if (!$groupId) {
            return null;
        } else {
            return array('Group' => array('id' => $groupId));
        }
    }

    public $validate = array(
        'id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
            ),
        ),
        'username' => array(
            'notempty' => array(
                'rule' => array('notempty'), 
                ),
        ),
        'password' => array(
            'notempty' => array(
                'rule' => array('notempty'),
            ),
        ),
        'name' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'Your name is required'
            )
        ),
        'surname' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'Your surname is required'
            )
        ),
        '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'
            )
        ),
        'group_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
            ),
        ),
    );

    public $hasMany = array(
        'Post' => array(
            'className' => 'Post',
            'foreignKey' => 'user_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

    public function bindNode($user) {
        return array('model' => 'Group', 'foreign_key' => $user['User']['group_id']);
    }

    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

这是因为您声明了 User 和 Post 之间的关系

public $hasMany = array(
    'Post'
);

如果您不需要这种关系,您也许可以$hasMany在您的 User 模型 ( ) 中删除这种关系。user.php

于 2013-02-13T16:44:19.833 回答