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 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python