2014-03-24 74 views
-1

我只是儘量節省輸入數據和保存線()方法告訴我的錯誤,這是我的控制寄存器方法:CakePHP 3.0 dev。模型保存()錯誤?

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

和錯誤:

Error: Call to a member function isNew() on a non-object File C:\wamp\www\vendor\cakephp\cakephp\src\ORM\Table.php Line: 1092

我會提到這一點,我確定我的模特名字'helloworlds' 有什麼想法?

+0

你確定'Helloworlds'是正確的?在所有其他命令中使用單數:'$ this-> Helloworld-> id;',$ this-> request-> data ['Helloworld']'... CakePHP命名約定(至少在2.x之前)以單數形式實施模型名稱,而不是複數形式。 – mathielo

+0

我使用Helloworld和非對象錯誤,並且我測試了''$ this-> modelClass'',所以? –

+0

對不起?我不明白你想說什麼。 – mathielo

回答

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