I think a combination of generateTreeList() and getPath() will get you 
there.

generateTreeList() can be used to get you the initial array.  Although I'm 
a bit leary of having the numeric id as the array key, since arrays with 
numeric keys tend to get orders numerically.  Let's pretend that's not an 
issue for the moment.

Then for each item in the array generated by the generateTreeList(), you 
could use getPath() to get the path, and then foreach the results to 
generate your list.

As a rough bit of pseudo, this should do the trick.

$list = $this->Model->generateTreeList();
foreach ($list as $key => $value) {
    $models = $this->Model->getPath($key);
    $path = '';
    foreach ($models as $model) {
        $path .= $model['Model']['name'] . ' > ';
    }
    $path = substr($path, 0, strlen($path) - 3); // strip of last ' > '
    $list[$key] = $path;
}

On Wednesday, 5 December 2012 05:04:41 UTC+10, Larry Lutz wrote:
>
> I'm working with Cake 2.2.4. I've learned perhaps more than I ever cared 
> to about nested sets and really appreciate how well Cake manages them. 
> Unfortunately, for all my research, I haven't been able to find a way to 
> create an array from my Category model that will give the full path for 
> each Category. Since, in my system, very different categories can have the 
> same name, the path (coupled with the ID for the category) is essential. I 
> think it comes across best in an illustration. Suppose my table looked like 
> this (without the lft, rght, and parent_id fields that are in the actual 
> table):
>
> ID   Category
>  1   CakePHP
>  2      Overview
>  3      Controllers
>  4         AppController
>  5      User Management
>  6         Overview
>  7         Profiles
>  8   CSS3
>  9      Overview
> 10      Forms
> 11      Canvas
> 12         Animation
>
> I'd like to be able to generate an array that looks like this:
>
>  1   CakePHP
>  2   CakePHP > Overview
>  3   CakePHP > Controllers
>  4   CakePHP > Controllers > AppController
>  5   CakePHP > User Management
>  6   CakePHP > User Management > Overview
>  7   CakePHP > User Management > Profiles
>  8   CSS3
>  9   CSS3 > Overview
> 10   CSS3 > Forms
> 11   CSS3 > Canvas
> 12   CSS3 > Canvas > Animation
>
> As you can see, just listing the category name, such as "Overview," can be 
> totally confusing. You need the path to make it intelligible. 
> generateTreeList() works great to generate a dropdown list, but if I need 
> to display the categories associated with a specific topic, it's not useful 
> at all; I need to display those as a single item for each topic like I 
> illustrate above. I've messed around with getPath() but the documentation 
> on it is scanty to say the least, and I can't seem to find any 
> illustrations of its use. In any case, it doesn't look getPath() will do 
> what I want.
>
> I suppose I could just create the categories as complete paths and avoid 
> the whole nested set issue altogether, but that kind of defeats the purpose 
> of having a dynamic database system.
>
> Any help or ideas would be greatly appreciated. I can't imagine why this 
> hasn't been dealt with in a plugin or behavior before. It seems to be a 
> fairly common use. In many ways, it's like breadcrumbs but dealing with a 
> series of records rather and single item.
>
>

-- 
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

--- 
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
Visit this group at http://groups.google.com/group/cake-php?hl=en.


Reply via email to