PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, February 21, 2022

[FIXED] `tree` behaviour for cakephp 3 not showing parent category in template view

 February 21, 2022     cakephp, cakephp-3.0, php     No comments   

Issue

I am using tree behaviour for cakephp 3 and baked model, controller, template through CLI it produced the listing for categories i have followed the table structure and data insertion is also exactly what i need. But in template view i have this code.

<?php  foreach ($categories as $category): ?>
<tr>
      <td><?php pr($category);?></td>
      <td><?= $this->Number->format($category->id) ?></td>
      <td><?= $category->has('parent_category') ? $this->Html->link($category->parent_category->name, ['controller' => 'Categories', 'action' => 'view', $category->parent_category->id]) : '' ?></td>
      <td><?= h($category->name) ?></td>
      <td class="actions">
        <?= $this->Html->link(__('View'), ['action' => 'view', $category->id]) ?>
        <?= $this->Html->link(__('Edit'), ['action' => 'edit', $category->id]) ?>
        <?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $category->id], ['confirm' => __('Are you sure you want to delete # {0}?', $category->id)]) ?>
     </td>
</tr>
<?php endforeach; ?>

This $category->has('parent_category') condition is showing blank it supposed to show parent category name i guess. Please let me know if anyone else has faced same problem. Any pointers to the docs will or anything will be highly appreciated. I have already gone though the official docs it didn't helped.


Solution

I found the problem and solution of the same. Seems Cake CLI doesn't write the whole code for you. Some Manual work is required.

Corrected Code Controller :

public function index()
{  
  //had to add this line for manually call the association
    $this->paginate['contain'] = ['ParentCategories'];
    $this->set('categories', $this->paginate($this->Categories));
    $this->set('_serialize', ['categories']);
}

In CakePHP 3.0 ContainableBehavior, recursive, bindModel, and unbindModel have all been removed. Instead the contain() method has been promoted to be a core feature of the query builder. Associations are only loaded if they are explicitly turned on. It's a small thing but had to go through the whole documentation to dig it out.



Answered By - valar morghulis
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,213,757

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing