当当前登录的用户创建/添加个人资料时,Iam尝试将值'1'保存到我的用户表的'profile_created'字段,我当前的实现不会更改此值,是否有任何建议?提前谢谢。
当前个人资料添加代码:
public function add() {
if ($this->request->is('post')) {
$this->Profile->create();
$this->request->data['Profile']['user_id'] = $this->Auth->user('id');
// code implemented below
$this->loadModel('User');
$data = array(
'Profile' => array('user_id' => $this->Auth->user('id')),
'User' => array('profile_created' => '1'),
);
if ($this->Profile->saveAssociated($this->request->data)) {
$this->Session->setFlash(__('Your account profile has been created'));
$this->redirect(array('controller' => 'events', 'action' => 'index'));
} else {
$this->Session->setFlash(__('Your account profile could not be saved. Please, try again.'));
}
}
}
答案 0 :(得分:2)
您无法更改$ this-> request-> data
中的值您还在创建$ data而不是在任何地方使用
尝试这样的事情(当然没有经过测试)
if ($this->request->is('post')) {
$this->Profile->create();
$data = $this->request->data;
$data['Profile']['user_id'] = $this->Auth->user('id');
// code implemented below
$this->loadModel('User');
if ($this->Profile->save($data)) {
//Update user here if Profile saved successfully
$this->Profile->User->id = $this->Auth->user('id');
if($this->Profile->User->saveField('profile_created', '1')){
$this->Session->setFlash(__('Your account profile has been created'));
$this->redirect(array('controller' => 'events', 'action' => 'index'));
}else{
$this->Session->setFlash(__('Your Profile was saved, but an error has occurred while updating the Users table'));
//Email your self the user ID here or something ??
}
} else {
$this->Session->setFlash(__('Your account profile could not be saved. Please, try again.'));
}
}
如果您需要进一步的帮助,请告诉我们!