2016-06-09 110 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