本文介绍了将数据从配置文件模型Cakephp保存到用户模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当目前登录的用户创建/添加配置文件时,Iam尝试保存值'1'到我的用户表的'profile_created'字段,我当前的实现不改变这个值,任何建议?提前感谢。
当前个人资料添加代码:
公共函数add(){
if($ this-> request-> is('post')){
$ this-> Profile-> create
$ this-> request-> data ['Profile'] ['user_id'] = $ this-> Auth-> user('id');
//下面实现的代码
$ this-> loadModel('User');
$ data = array(
'Profile'=> array('user_id'=> $ this-> Auth-> user('id')),
' '=> array('profile_created'=>'1'),
);
if($ this-> Profile-> saveAssociated($ this-> request-> data)){
$ this-> Session-> setFlash ('您的帐户资料已创建'));
$ this-> redirect(array('controller'=>'events','action'=>'index'));
} else {
$ this-> Session-> setFlash(__('无法保存您的帐户配置文件,请重试。
}
}
}
解决方案
您不能更改$ this-> request-> data
中的值。不在任何地方使用
尝试这样的测试(未测试)
if($ this-> request-> is('post')){
$ this-> Profile-> create();
$ data = $ this-> request-> data;
$ data ['Profile'] ['user_id'] = $ this-> Auth-> user('id');
//下面实现的代码
$ this-> loadModel('User');
if($ this-> Profile-> save($ data)){
//如果配置文件已成功保存,请在此处更新
$ this-& > User-> id = $ this-> Auth-> user('id');
if($ this-> Profile-> User-> saveField('profile_created','1')){
$ this-> Session-> setFlash(__帐户配置文件已创建'));
$ this-> redirect(array('controller'=>'events','action'=>'index'));
} else {
$ this-> Session-> setFlash(__('您的配置文件已保存,但更新Users表时发生错误)
//给你的电子邮件你的用户ID在这里或什么东西?
}
} else {
$ this-> Session-> setFlash(__('无法保存您的帐户配置文件,请重试。
}
}
如果您需要更多帮助, p>
Iam trying to save value '1' to the 'profile_created' field of my user table when the currently logged in user creates/adds a profile, my current implementation doesnt change this value, any suggestions? Thanks in advance.
Current profile adding code:
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.'));
}
}
}
解决方案
You can't change values in $this->request->data
You're also creating $data and not using it anywhere
Try something like this (not tested of course)
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.'));
}
}
Let me know if you need further help !
这篇关于将数据从配置文件模型Cakephp保存到用户模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!