Blog Tutorial Issue


#1

I am new to Cakephp and am working on the blog tutorial. Everything works fine except one part I can’t figure out. I just used a bake all for categories and articles pieces, but when you try to edit or add a new article, the drop down list is not populated with categories. It is just blank. How do I go about fixing this?

ArticlesController: (edit segment)

public function edit($id = null)
    {
        $article = $this->Articles->get($id, [
            'contain' => []
        ]);
        if ($this->request->is(['patch', 'post', 'put'])) {
            $article = $this->Articles->patchEntity($article, $this->request->getData());
            if ($this->Articles->save($article)) {
                $this->Flash->success(__('The article has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The article could not be saved. Please, try again.'));
        }
        $this->set(compact('article'));
        $this->set('_serialize', ['article']);
    }

ArticlesTable: relevant part

public function initialize(array $config)
    {
        parent::initialize($config);

        $this->setTable('articles');
        $this->setDisplayField('title');
        $this->setPrimaryKey('id');

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

#2

Took me a while to figure it out as this is just a side hobby, but I was missing a line:

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

Then this line:

$this->set(compact('article'));

need to be:

$this->set(compact('article', 'categories'));

All simple stuff if you know what you are doing…