Allows quick reordering of your CakePHP models. Supports models with a belongsTo relation, allowing to reorder data within a category.
The model must have a field (default: position
) which holds the order of the data. If the model has a belongsTo relation, this can be used to order the data within that category.
Put the PositionBehavior.php
file in the /App/Model/Behavior/
folder.
In your model include the behavior: public $actsAs = array('Position'=>array('categoryField'=>'category_id'));
. If no categoryField is used, that part can be ommited public $actsAs = array('Position');
, or supply null: public $actsAs = array('Position'=>array('categoryField'=>null));
. The default position field can also be changed with the field
attribute: public $actsAs = array('Position'=>array('field'=>'order', 'categoryField'=>'category_id'));
Simply call the model method $model->move($id, 'up');
. The data can be moved up/left or down/right. The first entry shall have position no. 1 (appears at the top/leftmost part of the dataset). This behavior overloads the model's onSave method to automatically add new data at the end of the data set.
Licensed under MIT license.