CakePHP LogoIf 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 or controller of your choice.

    /**
    * This is a maint function to recover/reorder the 
    * lft/rght columns of a tree model.
    * 
    */
    public function recover_tree( ) {
        $modelClass = $this->modelClass;
        if ( property_exists( $this->$modelClass->Behaviors, 'Tree' ) ) { 
            if ( !$this->$modelClass->recover() ) {
                echo 'Error recovering acl tree';
            } else {
                echo $modelClass . ' reordered.';
            }
        } else {
            echo $modelClass . ' is not a tree model';
        }
        exit;
    }

CakePHP’s Acls implementation also uses this same Tree table structure. Since Acos and Aros often don’t have their own controllers the following method will allow you to reorder the lft rght columns in one shot. Again, add this to your AppController or controller of choice.

    /**
    * This is a maint function to recover/reorder the
    *  lft/rght columns of the aco and aro models.
    * 
    */
    public function recover_acl_tree( ) {
        App::Import('Model', 'Aco' );
        App::Import('Model', 'Aro' );
        $Aco = new Aco;
        $Aro = new Aro;
        if ( !$Aco->recover() || !$Aro->recover() ) {
            echo 'Error recovering acl tree';
        } else {
            echo 'Aros and Acos reordered.'; 
        }
        exit;
    }

These methods/actions are meant to be called manually as you feel needed. If you would rather call them from other controller actions I would suggest prepending ‘_’ to the method names making them private/protected methods rather than url accessible actions.

cakephp tree reorder

Keywords:

  • rght