On Jun 8, 4:31 pm, Dima <[email protected]> wrote:
> First, sorry if subject name isn't accurate; I don't know how exactly
> to phrase what I'm searching for...

I know the situation. But yours seems apt.

> Here's the issue- I have a table which generates a menu for me.  The
> table has primary_key, name, parent_id, display, order_in_list.  Below
> is a sample table with display and order_in_list removed.
> primary  key
> ...
>
> Here's roughly what the menu should look like:
>
> -MenuItemA
>     -SubItemA
>     -SubItemB
> -MenuItemB
>     -SubMenuA
>         -SubItemC
>         -SubItemD
>
> This is what my model currently is:
>
> var $hasMany = array
> (
>         'ChildNavMenu'=>array
>         (
>                 'className'=>'Navmenu',
>                 'foreignKey'=>'parent_id',
>                 'conditions'=>array
>                 (
>                         'display'=>'1'
>                 ),
>                 'order'=>'order_in_list'
>         )
> );
>
> This correctly creates children of parents, BUT fails to create
> children of children of parents (SubItemC + SubItemD in example
> above.)  Does anybody know of a way to change the $hasMany in order to
> include this critical data?
>
> Thanks,
> Dima

You'd have to create a model for the child menu items, then create a
hasMany assoc. with grandchild items.
If it were up to me, I'd use MPTT [1] and TreeBehavior. That way, you
don't need to create child (or grandchild) models. This is especially
important if the branch may go deeper. You simply have one model for
all menu items and specify the parent_id, left, and right (see
explanation at the links below). The TreeBehavior makes using this
quite simple.

[1] Modified Preorder Tree Traversal
http://www.ad7six.com/entries/view/56/Working-with-Tree-data-%28MPTT%29
http://articles.sitepoint.com/article/hierarchical-data-database
http://dev.mysql.com/tech-resources/articles/hierarchical-data.html

There's also AD76's TreeHelper. Highly recommended.
http://bakery.cakephp.org/articles/view/tree-helper-1

It can be a bit confusing to set up the templates for the helper but
it works like gangbusters. Have a look at all this and let me know if
you need some help. I can give you a complete example using a Section
model (basically, a file system hierarchy) that works really well.


Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

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] For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to