6.4.3 Advanced Usage
ada perubahan tertangguh untuk seksyen ini. More information about translations
The tree behavior doesn't only work in the background, there are a number of specific methods defined in the behavior to cater for all your hierarchical data needs, and any unexpected problems that might arise in the process.
6.4.3.1 moveDown
ada perubahan tertangguh untuk seksyen ini. More information about translations
Used to move a single node down the tree. You need to provide the ID of the element to be moved and a positive number of how many positions the node should be moved down. All child nodes for the specified node will also be moved.
Here is an example of a controller action (in a controller named Categories) that moves a specified node down the tree:
function movedown($name = null, $delta = null) {
$cat = $this->Category->findByName($name);
if (empty($cat)) {
$this->Session->setFlash('There is no category named ' . $name);
$this->redirect(array('action' => 'index'), null, true);
}
$this->Category->id = $cat['Category']['id'];
if ($delta > 0) {
$this->Category->moveDown($this->Category->id, abs($delta));
} else {
$this->Session->setFlash('Please provide the number of positions the field should be moved down.');
}
$this->redirect(array('action' => 'index'), null, true);
}
function movedown($name = null, $delta = null) {$cat = $this->Category->findByName($name);if (empty($cat)) {$this->Session->setFlash('There is no category named ' . $name);$this->redirect(array('action' => 'index'), null, true);}$this->Category->id = $cat['Category']['id'];if ($delta > 0) {$this->Category->moveDown($this->Category->id, abs($delta));} else {$this->Session->setFlash('Please provide the number of positions the field should be moved down.');}$this->redirect(array('action' => 'index'), null, true);}
For example, if you'd like to move the "Sport" category one position down, you would request: /categories/movedown/Sport/1.
6.4.3.2 moveUp
ada perubahan tertangguh untuk seksyen ini. More information about translations
Used to move a single node up the tree. You need to provide the ID of the element to be moved and a positive number of how many positions the node should be moved up. All child nodes will also be moved.
Here's an example of a controller action (in a controller named Categories) that moves a node up the tree:
function moveup($name = null, $delta = null){
$cat = $this->Category->findByName($name);
if (empty($cat)) {
$this->Session->setFlash('There is no category named ' . $name);
$this->redirect(array('action' => 'index'), null, true);
}
$this->Category->id = $cat['Category']['id'];
if ($delta > 0) {
$this->Category->moveup($this->Category->id, abs($delta));
} else {
$this->Session->setFlash('Please provide a number of positions the category should be moved up.');
}
$this->redirect(array('action' => 'index'), null, true);
}
function moveup($name = null, $delta = null){$cat = $this->Category->findByName($name);if (empty($cat)) {$this->Session->setFlash('There is no category named ' . $name);$this->redirect(array('action' => 'index'), null, true);}$this->Category->id = $cat['Category']['id'];if ($delta > 0) {$this->Category->moveup($this->Category->id, abs($delta));} else {$this->Session->setFlash('Please provide a number of positions the category should be moved up.');}$this->redirect(array('action' => 'index'), null, true);}
For example, if you would like to move the category "Gwendolyn" up one position you would request /categories/moveup/Gwendolyn/1. Now the order of Friends will be Gwendolyn, Gerald.
6.4.3.3 removeFromTree
ada perubahan tertangguh untuk seksyen ini. More information about translations
removeFromTree($id=null, $delete=false)
Using this method wil either delete or move a node but retain its sub-tree, which will be reparented one level higher. It offers more control than delete(), which for a model using the tree behavior will remove the specified node and all of its children.
Taking the following tree as a starting point:
- My Categories
- Fun
- Sport
- Surfing
- Extreme knitting
- Skating
- Sport
- Fun
Running the following code with the id for 'Sport'
$this->Node->removeFromTree($id);
$this->Node->removeFromTree($id);
The Sport node will be become a top level node:
- My Categories
- Fun
- Surfing
- Extreme knitting
- Skating
- Fun
- Sport Moved
This demonstrates the default behavior of removeFromTree of moving the node to have no parent, and re-parenting all children.
If however the following code snippet was used with the id for 'Sport'
$this->Node->removeFromTree($id,true);
$this->Node->removeFromTree($id,true);
The tree would become
- My Categories
- Fun
- Surfing
- Extreme knitting
- Skating
- Fun
This demonstrates the alternate use for removeFromTree, the children have been reparented and 'Sport' has been deleted.
6.4.3.4 reorder
ada perubahan tertangguh untuk seksyen ini. More information about translations
This method can be used to sort hierarchical data.


























