How to get all many_many relations of a list

When you have a $many_many relation, e.g. Page m:n RelationTag, it's easy to get all the Tags of one specific Page. But when you want to get all Tags that are used by a bunch of Pages, how can you do that? Efficiently?

For this example I assume the same ORM structure as shown in this article.

Getting all tags of a list of blog post manually would require:

  • get the IDs of all Pages
  • loop over them
  • get the IDs of all RelationTags
  • get all Tags

which is just not performant cause we hit the database all the time. Better would be a double join from Page over Page_Relationtags table to RelationTag to get all that RelationTags used by this list of Pages. Fortunately SilverStripe's DataList class has a built in method for this, ->relation():

public function getAllTags()
    $pages = Page::get()->filter(/*whatever you want to filter here */);
    $allRelationTags = $pages->relation('RelationTags');

    return $allRelationTags;

Here we go!

Thanks to muskie9 on IRC for pointing me to the solution

Rate this post (3 rating(s))

Post your comment


No one has commented on this page yet.

RSS feed for comments on this page | RSS feed for all comments