我正在尝试编辑我的用户信息,我所做的唯一更改是在下面的控制器函数中将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']);
}
}
}
我有关于教程的帖子表,但我打算删除它们,因为它们与我的网站无关,但这与我收到此错误消息的事实有关
答案 0 :(得分:1)
这是因为您在
处声明了User和Post之间的关系public $hasMany = array(
'Post'
);
如果您不需要此关系,则可以在用户模型($hasMany
)中删除此user.php
关系。