【问题标题】:Saving data to User model from Profile model Cakephp从 Profile 模型 Cakephp 将数据保存到 User 模型
【发布时间】:2013-06-04 16:04:28
【问题描述】:

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


        }

【问题讨论】:

    标签: cakephp save cakephp-2.0 cakephp-2.1


    【解决方案1】:

    您不能更改 $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.'));
            }
        }
    

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

    【讨论】:

    • 您好 Mostafa,它不会更改相应用户模型行中的 profile_created 值,可能我的字段类型错误:'profile_created' int(10) unsigned NOT NULL,有什么想法吗?
    • 不,字段类型是正确的,但我建议将其从 int 更改为 tinyint,因为它只是一个标志。让我们看看,这个输出是什么? $this->Profile->User->read(null, $this->Auth->user('id')) 它是否也成功地在配置文件表中创建了配置文件?你能显示它存储的行吗?里面可能有提示!谢谢
    • 感谢 Mostafa,现在一切正常,我使用 $this->request->data 而不是 $data 并且我没有包含 $this->Profile->User->read(null, $this- >Auth->user('id')),而是使用 $this->Profile->User->id = $this->Auth->user('id');
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多