Blog: The Behavior Tour: Meet The Equal Nest Behavior

The Propel Team – 06 August 2012

Propel comes with a large set of behaviors but most of them are already well-known. However, we noticed that a lot of people didn't know about the awesome user contributions we have in the Propel community. In this series, we will introduce a new user contributed Propel behavior each week.

Ivan Tanev wrote a wonderful behavior named EqualNestBehavior to solve a common problem: relations between instances of a same class. The best example is probably a friendship relation between users/persons in your application. In a social networking world, it's a very common feature.

The behavior provides all the methods you need to manage those relations: addFriend(), removeFriend(), getFriends(), setFriends(), hasFriend(). See the complete list of ActiveRecord's methods for more details.

As usual, the ActiveQuery API has useful methods, too: you can either find or count friends for a given object.

Under the hood, this behavior defines a new table to store relations. It's like a Many to Many relationship, but with the same table referenced twice. Using this behavior is super easy, just add it to your XML schema as following:

The table name, friend, is important. It defines the relationship name, and everything is built around this name.

This behavior is really flexible, and works with Propel 1.6 all versions. It is fully unit tested, and hosted on GitHub: CraftyShadow/EqualNestBehavior. Thanks for this great behavior Ivan!