php - 在 CakePHP 树中查找子树

标签 php cakephp tree mptt

在 CakePHP 中,如何在 actsAs 树的模型中只选择一个子树?

我试过这个,找到以 label = "My Label"

项为首的树
$this->find("threaded", array(
    "conditions" => array(
        "label" => "My Label"
    )
));

...但是查看日志,它运行此 SQL:

SELECT Menu.id, Menu.parent_id, Menu.lft, Menu.rght, Menu.label, Menu.link
FROM menus Menu
WHERE label = 'My Label'

这显然只选择了一个节点,而不是它的所有子节点。

最佳答案

看来您必须像这样分两步完成(来自 the manual ):

$parent = $this->Category->find('first', array(
    'conditions' => array('label' => 'My label')
));
$parentAndChildren = $this->Category->find('threaded', array(
    'conditions' => array(
        'Category.lft >=' => $parent['Category']['lft'], 
        'Category.rght <=' => $parent['Category']['rght']
    )
));

你不能在 threaded 调用中使用 'label' => 'my label' 条件,因为它只会找到匹配该条件的结果, parent 和 children 。 'threaded' 仅根据 parent_id 重新排列正常查找操作的结果,因此您必须通过使用lft/right 列。

关于php - 在 CakePHP 树中查找子树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1598172/

相关文章:

bash - 从 TREE 命令输出创建目录和文件

php - HTML 呈现与 TCPDF(PHP)

php - concrete5中主题的目录结构

php - 在单列 MySQL 数据库中存储数组值

php - CakePHP 获取受影响的原始 SQL 语句行

algorithm - 从 Just Inorder Traversal 中查找预序?

mysql - SQL : How to select all parent nodes in Materialized path?

php - 具有可变参数的 Laravel 5.2 命名路由用法

php - 在 cakephp 的查询中使用 LIKE 条件

Cakephp 3.0 登录