Showing posts with label Behavior. Show all posts
Showing posts with label Behavior. Show all posts

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


Saturday, May 7, 2011

Sluggable Behavior - Generate Unique Slug

On the web, “slug” is a short text used in a URL to identify and describe a resource. Sluggable behavior is cakephp model behaviors that allow us to generate unique slug. Sluggable Behavior who created by eberfreitas is a simpler version of Mariano Iglesias’ Sluggable Behavior with a few add-ons. It’s basically the same thing but instead of implementing all the slug logic on the behavior, it just uses Cake’s Inflector::slug() method. With CakePHP 1.3, this method is really powerful and flexible.

To use it, just place the behavior file on the proper place (/app/models/behaviors/)and call it on the model you want to “slugify”:

var $actsAs = array('Sluggable');



To use it, just place the behavior file on the proper place and call it on the model you want to “slugify”: var $actsAs = array(‘Sluggable’); By default, this will automatically create slugs from a field named “title” and place it under a field named “slug”. If you want, you can customize everything. Checkout the configuration keys:

var $actsAs = array(
'Sluggable' => array(
'fields' => 'title',
'scope' => false,
'conditions' => false,
'slugfield' => 'slug',
'separator' => '-',
'overwrite' => false,
'length' => 256,
'lower' => true
)
);