在我的示例中调用非对象的成员函数save()

时间:2014-03-11 09:42:23

标签: cakephp

Db表名称:: 用户,drprofiles

模特用户,Drprofile

我在

中遇到错误
  

$这 - > invoice-> Drprofile->保存($这 - >请求 - >数据);

如何继续

<?php

class UsersController extends AppController {
    public $hassave=array(
'Work' => array(
                    'className' => 'Work',
                    'joinTable' => 'drprofiles',
                    'foreignKey' => 'user_id',

                )
                );

    public function beforeFilter() {
         parent::beforeFilter();

  $this->Auth->allow('add'); 




    }

    public function login() {
        if ($this->request->is('post')) {
            /* login and redirect to url set in app controller */
            if ($this->Auth->login()) {
                return $this->redirect($this->Auth->redirect());
            }
            $this->Session->setFlash(__('Invalid username or password, try again'));
        }
    }

    public function logout() {
         /* logout and redirect to url set in app controller */
        return $this->redirect($this->Auth->logout());
    }

   public function add() {
        if($this->request->is('post')) {
        $this->User->create();
        if ($invoice = $this->User->saveAll($this->request->data)) {
            //$this->Session->setFlash(_('The invoice has been saved.'), true);
            if(!empty($invoice)) {
                $this->request->data['Drprofile']['user_id'] = $this->User->id;

                $this->invoice->Drprofile->save($this->request->data);
                echo "InvoicesWork save completed?<br/>";
                echo var_dump($this->request->data);
            }
            $this->redirect(array('action' => 'index'));
        }
        else {
            $this->Session->setFlash(_('The invoice could not be saved.', true));
        }
    }



    }


}

在错误行之前,所有字段都正确地插入到users表中,但是在dbprofiles表中,它没有正确插入。

1 个答案:

答案 0 :(得分:0)

发票如小写字母i?

调用成员函数保存非函数通常是因为模型不在当前类中。所以你不能从不同的类中调用函数。

您可以在class YourController{}

下添加此代码
public uses = array('YourModel','YourModel2');

它的作用是包括YourModel和YourModel2,以便您可以在控制器中使用它。因此,您现在可以使用$this->YourModel->save()$this->YourModel2->save()