2016-06-09 24 views
0

嘗試POST到API時出現以下錯誤。我已經遵循this這本書的教程,所以我不確定爲什麼插入不起作用。CakePHP - CRUD API:插入到數據庫CakePHP時出錯

Message: Call to member function error() on boolean 
Trace: ControllerTrait.php 

我的添加功能是通過烘烤,但儘管如此,錯誤似乎發生在實體保存過程中。

public function add() 
    { 
     $author = $this->Authors->newEntity(); 
     if ($this->request->is('post')) { 
      $author = $this->Authors->patchEntity($author, $this->request->data); 
      if ($this->Authors->save($author)) { 
       $this->Flash->success(__('The author has been saved.')); 
       return $this->redirect(['action' => 'index']); 
      } else { 
       $this->Flash->error(__('The author could not be saved. Please, try again.')); 
      } 
     } 
     $this->set(compact('author')); 
     $this->set('_serialize', ['author']); 
    } 
+1

錯誤信息本身只是因爲flash組件尚未加載。 – AD7six

回答

1

你不需要add動作,只是將其刪除 - 這是CRUD插件不正是你。

如果你需要定製你需要return $this->Crud->execute()到底,例如CRUD操作:

public function add() 
{ 
    $this->Crud->on('beforeSave', function (Event $e) { 
    // Custom logic before save 
    }); 

    // Make sure CRUD takes care of the rest 
    return $this->Crud->execute(); 
} 

但是,是的,它會工作,如果你只是刪除add方法都在一起。

+1

非常感謝!我一直在掙扎幾個小時 – Andy