Issue
I am using cakephp 3.4.2 and am doing the blog tutorial to dive back into the new cakephp. I decided to follow the tutorial but to package it as a plugin.
There is an Articles Model and a Categories Model and we use the Tree Behavior on the CategoriesTable.php initialize() method.
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'
]);
}
When you call the treeList finder method in the CategoriesController.php add or edit function it works fine and the threaded list shows up nicely in the select list of the form.
The ArticlesController.php add and edit functions
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'));
}
throws an error:
Unknown finder method "treeList"
I have tried instantiating the Tree behavior in the ArticlesTable.php as I have done in the CategoriesTable.php (though I don't think that should be necessary) but that doesn't work - and the error persists. Any suggestions would be appreciated.
debug from debug($this->Articles->Categories->target());
`/plugins/Blog/src/Controller/Admin/ArticlesController.php (line 51) object(Cake\ORM\Table) {
'registryAlias' => 'Categories',
'table' => 'categories',
'alias' => 'Categories',
'entityClass' => '\Cake\ORM\Entity',
'associations' => [],
'behaviors' => [],
'defaultConnection' => 'default',
'connectionName' => 'default'
}`
ArticlesTable.php initialize function:
public function initialize(array $config)
{
$this->table('articles');
$this->displayField('title');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Categories', [
'foreignKey' => 'category_id',
]);
}
Solution
Thanks to ndm for pointing this out...
The belongsTo association should point to Blog.Categories
Answered By - Manu
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.