Re: Question Regarding Model Grandchildren
On Jun 9, 10:36 am, Dima <dmitriy.pind...@gmail.com> wrote:
> cricket - thanks for the suggestion. I've been reading up on MPTT,
> but the first link doesn't have the demo described on the site. I was
> wondering if you could post our example just so I can go through it
> and see how MPTT structured table calls are made.
First, you need to add parent_id, lft, and rght fields (the latter two
are usually spelled that way to avoid conflicts) to your table. Then
set them up according to how the tree should display. Later, the
TreeBehavior will take care of fetching them in the correct nested
order.
I usually use a root node that has parent_id = null to keep things
simple. All of the find calls will include a condition that parent_id
must not be null, so this item is never included in the results. It's
a "virtual menu item", for want of a better term.
That means that the parent_id column should allow null. The lft & rght
should be INT NOT NULL.
Based on your example, you'd have the following records (untested):
name - parent_id - lft - rght
root - null - 1 - 16
MenuItemA - 1 - 2 - 7
SubItemA - 2 - 3 - 6
SubItemB - 2 - 4 - 5
MenuItemB - 1 - 8 - 15
SubMenuA - 8 - 9 - 14
SubItemC - 9 - 10 - 13
SubItemD - 9 - 11 - 12
So your main menu items have parent_id = 1, which is the never-seen
root node of the tree. MenuItemA's children have parent_id = 2 because
that's what the parent's lft column is.
Note that parent_id is not necessarily the id of the parent item.
These 3 fields have nothing to do with the primary key.
Once you've got the records set up correctly, add TreeBehavior to the
model:
var $actsAs = array(
'Tree' => array(
'parent' => 'parent_id',
'left' => 'lft',
'right' => 'rght'
)
);
Next, add a method to the model to fetch the records as a tree. My
model here is Section and I also want each section's Item names. Leave
the contain part out if you don't need something like that. I'm just
including it to show that it can be done, if necessary. Item here has
nothing to do with your MenuItem names. It's just something involved
with my particular application.
public function threaded()
{
$filters = array(
'conditions' => array('Section.parent_id IS NOT NULL'),
'fields' => array('*'),
'order' => array('Section.lft' => 'ASC'),
'contain' => array(
'Item' => array(
'fields' => array('Item.id'),
'order' => array('Item.name' => 'ASC')
)
)
);
return $this->find('threaded', $filters);
}
Then grab it from within your controller. Because this is a navigation
menu, you'll want to cache the result, of course. I would suggest
getting the menu using requestAction from within an element that is
cached.
public function nav()
{
return $this->Section->threaded();
}
Debug the result to ensure that the tree looks correct. From there,
you can either build your menu list yourself or use Andy's TreeHelper.
Using that, your element might look like this:
<?php
$section_nodes = $this->requestAction('/sections/nav');
?>
<div id="nav">
<?php
echo $tree->generate(
$section_nodes,
array(
'element' => 'sections/nav_node',
'model' => 'Section'
)
);
?>
</div>
Make sure to include the helper in the controller's $helpers array.
The helper creates a nested unordered list for the nodes. It applies a
template (here, nav_node.ctp) to each node on the tree. Within that
helper, several variables are available, as shown in Andy's post:
$data // the row of data passed to the helper
$depth // depth in the current tree 1 = first item
$hasChildren // whether the current row has children or not
$hasVisibleChildren // whether the current row has Visible children or
not. Only relavent for MPTT tree data
$numberOfDirectChildren // only avaliable with recursive data
$numberOfTotalChildren // only available with MPTT tree data
$firstChild // whether the current row is the first of it's siblings
or not
$lastChild // whether the current row is the last of it's siblings or
not
These allow you to apply different classes to the link, if desired,
based on where the node falls on the tree.
So, your nav_node.ctp, at its most basic might look like this:
echo $html->link(
$data['Section']['name'],
array('controller' => 'sections', 'action' => 'view', 'id' =>
$data['Section']['id']),
array('title' => 'click to view this section')
);
But you can use the variables above to apply different classes, etc.
based on the position.
When you add a new Section, include a hidden field for the parent_id.
TreeBehavior will do the rest.
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 cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home