Sunday, September 22, 2013

Generate lft and rght values for a cakephp 2.0 tree



If you are using the Tree Behavior sometimes the lft and rght columns get out of sync, especially if you are adding and deleting records during development. Add the following method to your app/app_controller.php your choice.

1:  public function recover_tree() {  
2:      $modelClass = $this->modelClass;  
3:      $recovery = $this->$modelClass->recover();  
4:      if ($recovery) {  
5:        $this->Session->setFlash(__($modelClass . ' reordered.'), 'flash/success');  
6:        $this->redirect(array('action' => 'index'));  
7:      } else {  
8:        $this->Session->setFlash(__('Error recovering ' . $modelClass . ' tree'), 'flash/error');  
9:      }  
10:      exit;  
11:    }  

These methods/actions are meant to be called manually as you feel needed.
Examle : http://localhost/projects/categories/recover_tree


3 comments:

  1. Thank you so much for sharing this. I appreciate your efforts on making this collection.
    Hire Cakephp Developer

    ReplyDelete
  2. Tq for comment. Sorry because this article for CakePHP 1.3.

    ReplyDelete
  3. Nice Article, Thanks for writing such informative post.
    If your looking for cakephp interview questions please refer to
    Cakephp Interview Questions

    ReplyDelete