Cakephp 如果parent_id字段为空,如何获取父项的所有子项

Cakephp 如果parent_id字段为空,如何获取父项的所有子项,cakephp,cakephp-1.3,parent-child,cakephp-1.2,Cakephp,Cakephp 1.3,Parent Child,Cakephp 1.2,我有一个包含这些字段的分区表(id、parent\u id、lft、rght、name)。 当我发送$id时,它将检查parent_id字段,如果为null,它将搜索其子项。我应该如何实现这一点 我已经写了这段代码,但它不起作用 function view($id = null) { // find section in database $section = $this->Section->findById($id); // set the section

我有一个包含这些字段的分区表(id、parent\u id、lft、rght、name)。 当我发送$id时,它将检查parent_id字段,如果为null,它将搜索其子项。我应该如何实现这一点

我已经写了这段代码,但它不起作用

function view($id = null) {
    // find section in database
    $section = $this->Section->findById($id);
    // set the section for the view
    $this->set(compact('section'));
    // if find id  check parent_id field and if its is null then find his children
    if ($section['parent_id']== null){
        $this->Section->Article->children($id) ;
    } else {
        // else find parent only
        $this->Section->findbyid(parent);
    }
}

如果$id与任何父项\u id不匹配(该$id不是父项),那么它应该得到哪些子项?非常感谢您的回复,我将解释一些事情,我使用树行为,,但是我像这个表一样构建它>>看这个链接我在这里画了一个表所有父项必须为空字段而不仅仅是最上面的父项..我知道这是错误的,但我需要它来构建菜单..我使用帮助器..但是问题是如果用户点击娱乐(父项)查看音乐(子项)和电影(子项)中的所有文章,,所以我检查娱乐是否为空找到他的孩子如果所有的家长ID都为空,那么你就不再有一棵树了。你不能两者兼得。