List 如何在CakePHP中从find('threaded')创建select/options标记

List 如何在CakePHP中从find('threaded')创建select/options标记,list,cakephp,List,Cakephp,如何从CakePHP中的find'threaded'数据创建select/option html标记? 函数查找返回结果,如下所示: Array [0]=>阵列 [论坛]=>阵列 [id]=>1 [名称]=>论坛 ) [children] => Array ( [0] => Array ( [Fo

如何从CakePHP中的find'threaded'数据创建select/option html标记? 函数查找返回结果,如下所示:

Array
[0]=>阵列 [论坛]=>阵列 [id]=>1 [名称]=>论坛

            )

        [children] => Array
            (
                [0] => Array
                    (
                        [Forum] => Array
                            (
                                [id] => 3
                                [name] => Programowanie
                                [parent_id] => 1
                            )
                    )

                [1] => Array
                    (
                        [Thread] => Array
                            (
                                [id] => 11
                                [name] => Nowe forumowisko
                                [parent_id] => 1
                            )
                    )
            )
    )

[1] => Array
    (
        [Forum] => Array
            (
                [id] => 4
                [name] => Nauka
                [parent_id] => 0
            )

        [children] => Array
            (
            )
     )
)

怎么做?

孩子是什么?看起来你的树是跨一对多关系生成的


使用此方法不需要使用树行为,但必须能够在单个查询中找到所有所需的结果。

以下是对我有效的方法

确保更换:

{SELECT_ID}带有下拉列表的值 {SELECT_LABEL}带有显示为选项的内容 {MODEL_NAME} 用你的模特儿名字 这可能不是对每个人都100%有效,对我来说是有效的,但它应该能帮助你从一开始就有所收获。

谢谢,非常有用。。 我已将其修改为更通用:

/**
 * Returns an indented html select based on children depth
 *
 * @param array $data_array - Array of data passed in from cake's find('threaded') feature
 * @param array $model - the model name
 * @param array $key - the key field on the model
 * @param array $value - the value field on the model
 * @param array $list - Used internally, contains array to be returned
 * @param int $counter - Used Internally, counter for depth
 * @return array
 */
public function threaded_to_list($data_array, $model=null, $key='id', $value='name', 
&$list = array(), $counter = 0, $separator='__')
{
    if ( ! is_array($data_array))
        return array();

    foreach ($data_array AS $data)
    {
        $list[$data[$model][$key]] = str_repeat($separator, $counter).$data[$model][$value];
        if ( ! empty($data['children']))
        {
            $this->threaded_to_list($data['children'], $model, $key, $value, $list, $counter + 1);
        }
    }
    return $list;
}

有一种内置方法,用于将树状结果生成到选择选项标记的列表中:

$this->Model->generateTreeList($conditions, null, null, ' - ', $recursive);
而不是使用findthread

也就是说,如果您将树行为附加到它,那么您可能应该具有树行为,因为它显然是一个类似于树的模型

但如果您想保留findthread方法,则需要通过递归方法手动转换它

$this->Model->generateTreeList($conditions, null, null, ' - ', $recursive);