Thursday, September 09, 2010

dotCMS pull related content for transitive relationships

In dotCMS, it's easy enough to use the #pullRelatedContent() macro traverse a relationship to get a list of, say, all books related to author. But what if you wanted to traverse a chain of relationships? Let's say you have the following pair of transitive relationships: Author-Book and Book-Publisher, you have an Author and need to find what Publishers are related to her?

Here is how you do it. First, you pull related content from one relationship. Then you iterate through the results, pulling related content from the second relationship - merging the results into a separate list. You should then sort the list at the end.

## Author has books. Book has publisher.
#set($publishers = [])
#pullRelatedContent("Author-Book" "$authorInode" "0" "name")
#set($bookList = $list)
#foreach($book in $bookList)
   #pullRelatedContent("Book-Publisher" "$book.inode" "0" "title")
#set($list = $publishers)
#set($list = $sorter.sort($publishers, "name"))

After this, you might need to remove the duplicates.

Here is the post I made to the dotCMS Yahoo group about this: Compound relationship query.

My dotCMS notes.