Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Yii 如何从嵌套的父子结构创建路径_Yii_Parent Child - Fatal编程技术网

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

Yii 如何从嵌套的父子结构创建路径,yii,parent-child,Yii,Parent Child,我有一个具有此结构的类别模型: id,name,parent_id 我想创建一个面包屑样式的路径,但我不知道如何创建。您给我们的工作很少 下面的代码用于获取下拉树。您可以根据您的面包屑示例对其进行调整 /** * Create a tree dropdown based on the parent child relationships * * @param $parents Array of Category models to draw list for * @return ar

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

id,name,parent_id

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

您给我们的工作很少

下面的代码用于获取下拉树。您可以根据您的面包屑示例对其进行调整

/**
 * 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 . '---');
    }
}

到目前为止有相关代码吗?