TreeBehavior Articles & Posts

How to Create your own Scaffolding plugin for CakePHP 2

CakePHP’s builtin scaffolding is great but sometimes I wish it would do more or behave a little differently. Until recently, I assumed this couldn’t be accomplished with the way scaffolding was baked in. Thanks to the clever way CakePHP handles includes, we can easily use our own scaffolding implementation by adding 2 lines to AppController.

In this tutorial we will built a scaffolding plugin that is aware of the TreeBehavior (continue reading…)

Keywords:

  • pronique com
  • cakephp generate list scaffolding
  • cakephp 2 1 tutorial

CakePHP: Reorder lft rght columns in Tree models

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

Copyright © 1996-2012 PRONIQUE Software. All rights reserved.