dqx36753 2016-01-25 06:13
浏览 33
已采纳

cakephp 3的`tree`行为未在模板视图中显示父类别

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.

  • 写回答

1条回答 默认 最新

  • dourong6054 2016-01-25 11:20
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解