从Profile模型Cakephp将数据保存到用户模型

时间:2013-06-08 12:24:31

标签: cakephp save cakephp-2.0 cakephp-2.1

当当前登录的用户创建/添加个人资料时,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.'));
                }
            }


        }

1 个答案:

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

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