6.4.3 Advanced Usage

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

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($title = null, $delta = null) {
        $cat = $this->Category->findByTitle($title);
        if (empty($cat)) {
            $this->Session->setFlash('There is no category named ' . $title);
            $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' => 'show'), null, true);
    }
  1. function movedown($title = null, $delta = null) {
  2. $cat = $this->Category->findByTitle($title);
  3. if (empty($cat)) {
  4. $this->Session->setFlash('There is no category named ' . $title);
  5. $this->redirect(array('action' => 'index'), null, true);
  6. }
  7. $this->Category->id = $cat['Category']['id'];
  8. if ($delta > 0) {
  9. $this->Category->moveDown($this->Category->id, abs($delta));
  10. } else {
  11. $this->Session->setFlash('Please provide the number of positions the field should be moved down.');
  12. }
  13. $this->redirect(array('action' => 'show'), null, true);
  14. }

For example, if you'd like to move the "Cookies" category one step down, you would request: /categories/movedown/Cookies/1.

6.4.3.2 moveUp

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($title = null, $delta = null){
        $cat = $this->Category->findByTitle($title);
        if (empty($cat)) {
            $this->Session->setFlash('There is no category named ' . $title);
            $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);
    
    }
  1. function moveup($title = null, $delta = null){
  2. $cat = $this->Category->findByTitle($title);
  3. if (empty($cat)) {
  4. $this->Session->setFlash('There is no category named ' . $title);
  5. $this->redirect(array('action' => 'index'), null, true);
  6. }
  7. $this->Category->id = $cat['Category']['id'];
  8. if ($delta > 0) {
  9. $this->Category->moveup($this->Category->id, abs($delta));
  10. } else {
  11. $this->Session->setFlash('Please provide a number of positions the category should be moved up.');
  12. }
  13. $this->redirect(array('action' => 'index'), null, true);
  14. }

For example, if you would like to move the category "Desserts" up one level up you would request /categories/moveup/Desserts/1.

6.4.3.3 removeFromTree

6.4.3.4 reorder

This method can be used to sort hierarchical data.