在 CakePHP 树中查找子树

发布于 2024-08-08 07:40:17 字数 448 浏览 4 评论 0原文

在 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'

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

In CakePHP, how do you select just a subtree in a model which actsAs tree?

I tried this, to find the tree headed by the item with label = "My Label"

$this->find("threaded", array(
    "conditions" => array(
        "label" => "My Label"
    )
));

...however looking at the logs, it runs this SQL:

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

Which obviously only selects the one node, and not all its children.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流殇 2024-08-15 07:40:17

看来你必须像这样分两步完成(来自手册):

$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']
    )
));

不能使用 'label' =>; 线程 调用中的“my label” 条件,因为它只会查找与该条件(父级和子级)匹配的结果。 'threaded' 仅根据 parent_id 重新排列正常查找操作的结果,因此您必须使用lft/rght 列。

It seems you will have to do it in two steps like this (from 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']
    )
));

You can't use the 'label' => 'my label' condition in the threaded call, since it would only find results that match that condition, parents and children. 'threaded' only rearranges the results of a normal find operation based on the parent_id, so you'll have to supply your own condition of what "children" are by using the lft/rght columns.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文