sfPropelActAsSortableBehaviorPlugin group patch
February 20th, 2009 by Carlos BarrosI guess that most Symfony developers should know this Symfony plugin, but for those who not, here’s an small quote:
“The sfPropelActAsSortableBehaviorPlugin is a symfony plugin that provides a new Propel behavior.Model classes with this behavior enabled become sortable, which means that they have new methods to deal with a position attribute.”
This Symfony plugin is really useful as it can save you a lot of code if you need to sort your objects, but in a recent project I worked on, I noticed this plugin lacks one functionality: it can’t handle “sorting groups” within the same object. For instance, suppose you have an Product and Category tables, and that each product belongs to a single category. Then, suppose you want to be able to sort your products so you can control how they show up in your pages. Using sfPropelActAsSortableBehaviorPlugin you can only sort the entire Product table, and that’s not really what we want. We need to be able to sort products within each category.
I guess this problem is really common, and as I had to handle this in a recent project, I decided to write a patch for this plugin, and I’d like to share it. Before one ask me why I didn’t submit it as a official ticket, the reason is that there is already a patch submitted to handle this issue, from almost a year ago, that was not released yet, and I bet most people (including me) is not aware of, so I’m writing this post to tell about this for other users, and maybe make Kris Wallsmith (current leader) release his version, that in fact seem to be more powerful than mine, as it support creating sorting groups using more than one column.
So, how does it work?? sfPropelActAsSortableBehaviorPlugin is really simple to use, all you need is a Integer column in your model, and then activate the new behavior for you class using something like this:
sfPropelBehavior::add('Item', array('act_as_sortable' => array('column' => 'rank')));
After applying this patch, the only thing you need to do is to add one more element to the above array:
sfPropelBehavior::add('Item', array('act_as_sortable' => array('column' => 'rank','group' => 'category_id')));
And that’s it. New sfPropelActAsSortableBehaviorPlugin will use “category_id” as a “sorting group”.
You can now use all methods provided by the new behavior to sort your objects, just like you used to do without the patch. Also, if you don’t have a group category, just omit it from behavior initialization, and it will work as if u didn’t apply this patch.
Find below the full patch:



