yii 如何从嵌套的父子结构创建路径

x759pob2  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(144)

我有一个具有以下结构的类别模型:

id,name,parent_id

我想创建一个面包屑样式的路径,但我不知道如何创建。

8qgya5xd

8qgya5xd1#

你给我们的证据太少了。
下面的代码用于获取下拉树。您可以修改它以适合您的breadcrumbs示例。

/**
 * Create a tree dropdown based on the parent child relationships
 *
 * @param $parents  Array of Category models to draw list for
 * @return array listitem with populated tree.
 *
 * @access public
 */
public function makeDropDown($parents)
{
    global $listItems;
    $listItems = array();
    $listItems['0'] = '== Choose a Category ==';
    foreach ($parents as $parent) {
        $listItems[$parent->category_id] = $parent->category_name;
        $this->subDropDown($parent->categories);
    }
    return $listItems;
}

/**
 * Create a tree dropdown based of a child
 *
 * @param $children  Array of children models to draw list for
 * @param $space  String identation string
 * @return array listitem with populated tree.
 *
 * @access private
 */
private function subDropDown($children, $space = '---')
{
    global $listItems;

    foreach ($children as $child) {

        $listItems[$child->category_id] = $space . $child->category_name;
        $this->subDropDown($child->categories, $space . '---');
    }
}

相关问题