CakePHP 3.0开发。模型Save()错误?

时间:2014-03-24 20:31:20

标签: php cakephp

我只是尝试保存输入数据并且在save()方法中显示错误,这是我的控制器注册方法:

public function register() {
    if ($this->Helloworlds->save($this->request->data)) {
        $id = $this->Helloworld->id;
        $this->request->data['Helloworld'] = array_merge($this->request->data['Helloworld'], array('id' => $id));
        $this->Auth->login($this->request->data['Helloworld']);
        return $this->redirect('/helloworld/index');
    }
    else
    {
        $this->Session->setFlash(__('Sorry no data has saved '));
    }
} 

和错误:

  

错误:在非对象文件上调用成员函数isNew()   C:\ wamp \ www \ vendor \ cakephp \ cakephp \ src \ ORM \ Table.php行:1092

我会提到这个我确定我的模特名称'helloworlds' 任何想法?

1 个答案:

答案 0 :(得分:0)

至少我找到答案,真正的代码是:

public function register() {

        if ($this->request->is('post')) {   
      $Helloworld = TableRegistry::get('helloworlds');    $Helloworlds =  $Helloworld->newEntity($this->request->data);   if ($Helloworld->save($Helloworlds)) {
        $id = $this->Helloworld->id;
        $this->request->data['Helloworld'] = array_merge($this->request->data['Helloworld'], array('id' => $id));
        $this->Auth->login($this->request->data['Helloworld']);
        return $this->redirect('/helloworld/index');
    }   else    {       $this->Session->setFlash(__('Sorry no data has saved '));   }   } }