sfPropelActAsSortableBehaviorPlugin - 0.6.0

Sortable behavior for Propel.

You are currently browsing
the website for symfony 1

Visit the Symfony2 website

« Back to the Plugins Home


Forgot your password?
Create an account



advanced search
Information Readme Releases Changelog Contribute
This plugin is deprecated and is not maintained anymore. Please use sfPropel15Plugin instead - Propel 1.5 bundles sortable behavior in core
Show source | Show as Markdown

sfPropelActAsSortableBehaviorPlugin plugin

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.


  • Install the plugin

    $ symfony plugin-install http://plugins.symfony-project.com/sfPropelActAsSortableBehaviorPlugin

  • Check that the model(s) that you want to make sortable have a position column (must be an integer). If there is none, add it in the schema.yml.

    item: _attibutes: { phpName: Item } id: name: varchar(50) rank: integer

  • Enable Propel behavior support in propel.ini:

    $ propel.builder.AddBehaviors = true

    If you have to enable the behavior support, rebuild your model:

    $ symfony propel-build-model

  • Activate the behavior for one of your Propel models, specifying the name of the position column (rank in this example):

    // lib/model/Item.php class Item extends BaseItem { // whatever }

    sfPropelBehavior::add('Item', array('act_as_sortable' => array('column' => 'rank')));

  • Optionnally, you can add three new methods to the Peer class of your model. To do so, just copy and paste the following code inside the Peer class:

    public static function retrieveByPosition($position, $con = null) { return sfPropelActAsSortableBehavior::retrieveByPosition(CLASS, $position, $con); }

    public static function getMaxPosition($con = null) { return sfPropelActAsSortableBehavior::getMaxPosition(CLASS, $con); }

    public static function doSelectOrderByPosition($order = Criteria::ASC, $criteria = null, $con = null) { return sfPropelActAsSortableBehavior::doSelectOrderByPosition(CLASS, $order, $criteria, $con); }

    public static function doSort($order, $con = null) { return sfPropelActAsSortableBehavior::doSort(CLASS, $order, $con); }


Upon saving an object declared sortable, its position is automatically set to the next position available, except if the position attribute was already set prior to saving.

Whatever the name of the position column defined in the behavior activation, you can get the value of the position of a sortable object through the getPosition() method.

$item1 = new Item();
echo $item1->getPosition();
 => 1
$item2 = new Item();
echo $item2->getPosition();
 => 2

The behavior adds the following methods to the extended model classes:

  • getPosition(): Gets the position of a sortable object

  • setPosition($position): Sets the position of a sortable object. Beware that there is no check made on the value passed. If the position already exists, or if it is superior to the highest position + 1, the method does not throw any exception

  • getNext(): Returns the next item in the list, i.e. the one for which position is immediately higher

  • getPrevious(): Returns the previous item in the list, i.e. the one for which position is immediately lower

  • isFirst(): Checks if the object is first in the list, i.e. if it has 1 for position

  • isLast(): Checks if the object is last in the list, i.e. if its position is the highest position

  • moveUp(): Moves the object higher in the list, i.e. exchanges its position with the one of the previous object

  • moveDown(): Moves the object lower in the list, i.e. exchanges its position with the one of the next object

  • swapWith($object): Exchanges the position of the object with the one passed as argument

Behaviors cannot automatically extend the Peer classes (due to limitations of PHP). However, if you followed the optional installation instruction, the Peer class of a sortable model can also have new static methods:

  • retrieveByPosition($position, $con = null): Returns an item from the list based on its position

  • getMaxPosition($con = null): Returns the highest position of a class of sortable objects

  • doSelectOrderByPosition($order = Criteria::ASC, $criteria = null, $con = null): Returns an array of sortable objects ordered by position

  • doSort($order, $con = null): Reorders a set of sortable objects based on a list of id/position. Beware that there is no check made on the positions passed, so incoherent positions will result in an incoherent list.

If you don't want to copy the code, you can still use the static methods of the sfPropelActAsSortableBehavior class, with an additional parameter at the beginning being the Peer class name.


  • add more documentation and usage examples
  • add moveToPosition($position), moveToTop() and moveToBottom() methods
  • add insertAtPosition($position) method
  • add removeFromList() method to remove an element from the list without deleting it