A Primer on Relationships (with EE)
by Noah Stokes
It’s about time we got serious and defined this relationship. You know the one where we pair a couple of ExpressionEngine entries together like an eHarmony match made in nerd heaven. If you’ve never dabbled in the ways of Cupid, then it’s time you learn. Relationships or Related Entries in ExpressionEngine are a simple and powerful way to reuse existing data and avoid the ultimate no-no: duplicate data.
One of the best bits of advice that I received as a young developer was to make sure that my data was only in the database once. Database optimization, only editing data in one place, the reasons go on and on, and are perhaps best saved for an article in _DBA’s Rule The World_ magazine. What about us EE users though? How can we be cautious about duplicate data by taking advantage of EE Relationships? How does it benefit us, and more importantly our end users?
I’m a learn by doing kind of guy, so let’s jump right into a real world example.
For Real Estate
I recently built a website for a local real estate team. ExpressionEngine was the obvious choice to drive the backend as we were able to customize fields to our hearts content. We built custom fields for the obvious data: property price, number of bedrooms/bathrooms, etc. We also wanted to include some data that would be consistent through out nearly every entry, like city and related properties.
We could have easily added a custom text field for the city and make the user type in the city name and be done with it. Similarly, we could have added an FF Matrix field and let the user type in a property address and URL for other related properties. Both of those solutions would have resulted in duplicate data in our database. Rather than duplicating the data in every entry we needed to create relationships between the data. This would keep our database free of duplicates and create a more streamlined interface for our end user.
Relationships are Fun
The Related Entries functionality in ExpressionEngine is a simple, powerful and elegant solution to create relationships between entries in different (and the same) channels, avoiding duplicate data and creating a pleasant end user experience.
However, if you are looking to take these relationships to the next level (second base y’all) then you may want to check out Pixel & Tonic’s fieldtype, Playa. Playa is the diamond ring shopping in EE relationships. If you’ve never heard of Playa, you should read up on it first and then come back. If you’re already familiar with Playa, I’m going to jump in directly with showing you how I used it. This will hopefully give you a better understanding of what it does and more importantly how it solves the problem of duplicate data.
Let’s start with the city custom field that we’re using to display city data on our Featured Properties entires. I wanted to include more than just a basic city name with each entry. I wanted to add data like population, county and school districts. More importantly, I only wanted the end user to have one place to update all of this information and have it propagate throughout the site.
First, I created a channel for Cities and filled it with all of the custom fields I needed. Then I simply created a relationship between my Properties channel entries and the Cities channel entries. The Entry Edit form then looked like this:

Drop-down menu to choose city.
A simple dropdown menu allows the end user to select a city, and thanks to the relationship the output looked like this:

How the city displays on the front-end of the website.
The map graphic, population, county and link to more information on the city were all data from a Cities channel entry that was pulled into our Featured Property entry via a relationship. This simple relationship displayed a significant amount of data all with an end user scenario that could not be easier: a simple dropdown selection. What’s more, is that if this cities population changes, for example, there is only one place to update it, and it will propagate through the entire website.
Let’s move on to another level of relationships, third base.
Relating entries within the same channel
A requested feature for this site was to enable the end user to link to related properties from a Featured Property entry. For example, I could link a property in the city of Alamo to other properties for sale in Alamo, giving our prospective buyer other properties to view that may interest them. As I mentioned before, a simple solution for this could have been to use the FF Matrix fieldtype and allow the end user to add an address and URL to each Featured Property entry. This would have been both tedious and time consuming and would have once again created duplicate data through our build.
Our solution was to create a custom Playa field type in our Featured Property channel called Other Properties. We set the channel to relate to itself, Featured Property. What this gave us was a listing of all our Featured Property entries:

Other Properties custom field using Playa.
Now, I could simply drag and drop as many existing Featured Property entries into the new Featured Property entry that I was creating; relating multiple entires to one specific entry on the backend and creating a dead simple interface for the end user to work with. The resulting display on the page looked like this:

Front-end display of Other Properties data.
This solution couldn’t be simpler for the end user—and for someone who strives to make the end user experience exceptional, there is nothing nicer. Additionally, from a developer perspective, it couldn’t be easier to create these relationships using EE’s built in Related Entries capabilities and/or Playa.
ExpressionEngine is such a powerful solution for driving websites that at times it’s almost too easy to simply create custom fields to fit our needs without giving much thought to the idea of duplicate data. If you can wrap your mind around some of these concepts I’ve shown as examples, I’m sure that you can begin to think of several powerful ways to use relationships in your own EE builds.
If you’re already a relationship power user, I’d love to hear some of your examples of how you use relationships in the comments below.
Post to Twitter
Jérôme Coupé — 06:50 on 07.06.2010
Great post!
If I have to create a relationship between two items in a website, I always look at these various options:
1. Categories: If these items share the same category group, you can make relationships between them just using their shared categories.
2. Native EE relationships: I do not use them much, since I find the UI (simple drop down) problematic as soon as you get more than a few entries. Perfectly good option if you only have a small bunch of entries to relate. I hope the UI will get better over time in EE2
3. Playa: I don’t think I can thank Brandon enough for that one. The UI is a joy to use (filters) and it solves many of the challenges at hand. Combined with {reverse_related_entries}, its power goes through the roof (although there are a few shortcomings related to reverse related entries). Making a Playa field a Gypsy field also helps creating site-wide relationships capabilities using the 1.6 branch of ExpressionEngine.
Kevin Selles — 12:55 on 07.10.2010
Nice article.
This is one of the reasons why I left WordPress for ExpressionEngine. WordPress had custom fields, but no way to create relationships between entries, so I found myself typing the same information over and over again (i.e. the videogame’s title). With EE I created a weblog/channel called Videogames where each entry is a different title, with information about the release date, the publisher, developer, etc. (many of these are also relationships to other channels). So when I’m writing an article, posting a video or whatever, I can pull the info of the game. If the game’s title or launch date changes, that info is updated all over the site.
I think relationships play a big role on websites, and I can’t imagine creating one without the help of Playa and Matrix. They’re both extremely useful tools to aid the development and functionality of our site.
Having read this article I’m glad to see that the method I chose to set up my channels was the correct one. :D
Noah Stokes — 14:56 on 07.10.2010
Thx for the feedback fellas.
Can’t agree with you more Kevin re: relationships playing big roles in websites.
Martin Luff — 19:20 on 07.12.2010
Thanks for the article - definitely one of the more powerful features of EE.
I notice there’s no mention of ‘Related Entries’ add-on from Solspace? My recollection is that this used to be more efficient than Playa? Either way, I’d recommend taking a look before deciding which best suits your needs http://solsp.ac/e0E
All the best…
pablo Boerr — 18:11 on 08.01.2010
Great Article Noah,
Kristen Grote — 15:09 on 10.18.2010
Thanks for this post Noah, it definitely clears some things up.
I noticed in your screenshot of the City dropdown that the list of cities appears to be separated by categories in bold gray text. Is that a Playa feature? How is that achieved?
Noah Stokes — 18:39 on 10.18.2010
Hi Kristen - Yes, that is a Playa field. I’m assuming that Playa puts that text there, I know it’s nothing I did to place it there.