2016-11-09 6 views
1

cakephp 3.xのブログチュートリアルを拡張したいと思います。Cakephp Blogカテゴリと記事を同時に追加する

私は記事を追加する記事/記事を追加してカテゴリを選択するか、新しいカテゴリを作成して新しい記事にリンクさせることができます。

記事/ add.ctpで、このお持ちの場合:ArticlesContoller.phpで

<h1>Add Article</h1> 
<?php 
    echo $this->Form->create($article); 
    echo $this->Form->input('title'); 
    echo $this->Form->input('category_id'); 
    echo $this->Form->input('name'); 
    echo $this->Form->input('body', ['rows' => '3']); 
    echo $this->Form->button(__('Save Article')); 
    echo $this->Form->end(); 
?> 

そしてこの

public function add() 
    { 
    $article = $this->Articles->newEntity(); 
    if ($this->request->is('post')) { 

     if($this->request->data['category_id']==""){ 




    $this->Categories->add($category); 



     } 

     $article = $this->Articles->patchEntity($article, $this->request->data); 
     // Added this line 
     $article->user_id = $this->Auth->user('id'); 
     // You could also do the following 
     //$newData = ['user_id' => $this->Auth->user('id')]; 
     //$article = $this->Articles->patchEntity($article, $newData); 
     if ($this->Articles->save($article)) { 
      $this->Flash->success(__('Your article has been saved.')); 
      return $this->redirect(['action' => 'index']); 
     } 
     $this->Flash->error(__('Unable to add your article.')); 
    } 
    $this->set('article', $article); 

     $categories = $this->Articles->Categories->find('treeList'); 
     $this->set(compact('categories')); 

    // Just added the categories list to be able to choose 
    // one category for an article 
    // $categories = $this->Articles->Categories->find('treeList'); 
    //$this->set(compact('categories')); 
    } 

を追加するためにしかし、私はCategories-で "不明なメソッド" のエラーが表示されます>追加

カテゴリを同時に追加するにはどうすればよいですか?

答えて

0

これは、テーブルカテゴリにメソッドがないことを意味します。 src/Model/Table/CategoriesTable.phpで次のようにメソッドを作成します。

public function add($data) 
{ 
$category = new Entity(); 
$category->... 
//add columns what you need to save in table 
if($this->save($category)){ 
return true; 
}else{ 
return false; 
} 
} 
関連する問題