Tag: CakePHP

CakePHP: Reorder lft rght columns in Tree models

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 action function 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 function 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 methods rather than url accessible actions.


Starting a CakePHP 2.0 Project with cakeinit

The latest version of cakeinit (0.8.5) now includes support to create CakePHP 2.0 projects. To create a new project you only need two commands. This is an alternative to downloading the zip or tar.gz file and extracting.

# download cakeinit
curl http://cakeinit.pronique.com/cakeinit.php -o cakeinit.php
# Create an app called mycakeapp based on the cakephp-2.0 bundle
php ./cakeinit.php cakephp-2.0 mycakeapp

The cakephp-1.3 bundle has also been updated to pull the recently released CakePHP 1.3.11 version of the framework. If you get some ugly permission errors don’t forget to prepend sudo to the above commands.

Read more about cakeinit at http://cakeinit.pronique.com.


Guide to CakePHP 2.0 Conventions

With the release of CakePHP 2.0 you will see some big changes. With the release of 2.0 alpha I decided it was time to start tinkering and getting ready for 1.x migrations and will start new development with 2.x. This page serves as a quick intro and reference to these changes. If you are familiar with 1.x I’ve provided some side-by-side comparison of code. (continue reading…)


CakePHP Developer Links

This page was started to store and organize a collective of CakePHP tutorials and useful links for PHP developers. Currently there are 122 articles and counting

(continue reading…)


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