2017-06-03 26 views
0

私はcakephp 3.4.2を使用していて、blog tutorialを実行して新しいcakephpに戻っています。チュートリアルに従いますが、プラグインとしてパッケージ化することにしました。関連モデルのcakephp 3ツリーの動作

記事モデルとカテゴリモデルがあり、CategoriesTable.php initialize()メソッドのツリー動作を使用します。

public function initialize(array $config) 
{ 
    $this->table('categories'); 
    $this->displayField('name'); 
    $this->primaryKey('id'); 

    $this->addBehavior('Tree'); 

    $this->belongsTo('ParentCategories', [ 
     'className' => 'Blog.Categories', 
     'foreignKey' => 'parent_id' 
    ]); 
    $this->hasMany('Articles', [ 
     'foreignKey' => 'category_id', 
     'className' => 'Blog.Articles' 
    ]); 
    $this->hasMany('ChildCategories', [ 
     'className' => 'Blog.Categories', 
     'foreignKey' => 'parent_id' 
    ]); 
} 

あなたはそれが正常に動作し、スレッドのリストは、フォームの選択リストできれいに表示さ追加や編集機能CategoriesController.phpでtreeListファインダーメソッドを呼び出し

ArticlesController.phpが

public function add() 
{ 
    $article = $this->Articles->newEntity(); 
    if ($this->request->is('post')) { 
     $article = $this->Articles->patchEntity($article, $this->request->getData()); 
     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')); 
} 

を追加および編集機能は、エラーがスローされます。

Unknown finder method "treeList" 

私はCategoriesTableに行ったようArticlesTable.phpでツリーの振舞いをインスタンス化しようとしています。 PHP(ただし、私はそれが必要ではないと思う)しかし、それは動作しません - とエラーが続く。 何か提案がありがとうございます。

デバッグからdebug($this->Articles->Categories->target());

`/plugins/Blog/src/Controller/Admin/ArticlesController.php(ライン51) オブジェクト(ケーキ\ ORM \表){

'registryAlias' => 'Categories', 
'table' => 'categories', 
'alias' => 'Categories', 
'entityClass' => '\Cake\ORM\Entity', 
'associations' => [], 
'behaviors' => [], 
'defaultConnection' => 'default', 
'connectionName' => 'default' 

}`

ArticlesTable.phpの初期化関数:

public function initialize(array $config) 
{ 
    $this->table('articles'); 
    $this->displayField('title'); 
    $this->primaryKey('id'); 

    $this->addBehavior('Timestamp'); 

    $this->belongsTo('Categories', [ 
     'foreignKey' => 'category_id', 
    ]); 

} 
+1

を指している必要があります...それはそれを見ることなく任意の助言を与えることは難しいです。 – ndm

+0

それは私の愚かだった - 申し訳ありません。 add関数で質問を編集しました。謝罪。 – Manu

+1

'$ this-> Articles-> Categories'のように見えますが、それはあなたが期待するものではありません。 '$ this-> Articles-> Categories-> target()'をデバッグした場合、具体的な 'ArticlesTable'クラスの代わりに' \ Cake \ ORM \ Table'のインスタンスであることがわかりますか? 'ArticlesTable'クラスの' initialize() 'メソッドはどのように見えますか? – ndm

答えて

0

感謝これを指摘するためにNDMする...

belongsTo関連は、あなたの投稿がエラーをトリガーする実際のコードが含まれていませんBlog.Categories

関連する問題