【问题标题】:How to display treeview in view using cakephp 2如何使用 cakephp 2 在视图中显示树视图
【发布时间】:2021-09-20 00:25:49
【问题描述】:

enter image description here大家好,我在 cakephp 2 中显示树视图时遇到问题...使用与父母和孩子相同的表作为关系。

表格媒体目录

id Title Parent_id
1 Test 1 null
2 Test 2 null
3 Test 3 1
4 Test 4 1
5 Test 5 3
6 Test 6 2
7 Test 7 2

ContentsController.php

public function index()
{
    $Treeview = $this->MediaDirectory->find("all", 
        array(
            'conditions' => array(
                'MediaDirectory.parent_id' => null
            )
        )
    );
    
    $this->set(compact('$Treeview'));
}

【问题讨论】:

标签: php cakephp cakephp-2.0


【解决方案1】:

首先我不明白你怎么能在你的表中有 2 次名为 parent_id 的字段。您应该删除为空的那个。 您的表中需要 3 个不同的字段:

  • parent_id 用于存储父对象的 id。
  • lft 存储当前行的 lft 值。
  • rght 存储当前行的正确值。

然后您需要在模型中添加以下行:

public $actsAs = array('Tree');

如果你想让它工作,你必须使用你的控制器插入你的数据。当您在其中保存一些数据时,内核会自动定义 lft 和 rght。

例如,在您的控制器中

$data['MediaDirectory']['parent_id'] = 10;
$data['MediaDirectory']['title'] = 'Test 1';
$this->MediaDirectory->save($data);

然后你应该像这样替换你的控制器中的代码:

public function index() {
  $Treeview = $this->MediaDirectory->find("threaded", 
    array(
      'conditions' => array(
        'MediaDirectory.parent_id'=> null,
        'MediaDirectory.fk_id'=> $cid // Not sure what this is suppose to do
      )
    )
  );
  $this->set(compact('Treeview')); // TreeviewChild name was wrong
}

查询构建 find("threaded") 假设返回树数据。不要犹豫,使用 debug() 函数来可视化您当前正在处理的数据。

关于 2.x 版本,您需要的一切都在那里:https://book.cakephp.org/2/en/core-libraries/behaviors/tree.html

【讨论】:

  • 嗨,先生,如何在树视图中显示
猜你喜欢
  • 2022-11-16
  • 1970-01-01
  • 2022-01-10
  • 2018-11-22
  • 1970-01-01
  • 1970-01-01
  • 2018-09-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多