2

当当前登录的用户创建/添加配置文件时,我试图将值“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.'));
                }
            }


        }
4

1 回答 1

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

如果您需要进一步的帮助,请告诉我!

于 2013-06-08T19:21:04.547 回答