Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Returning Categories with at least one Article: better, prettier way?

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 461 Views
  • GuillaumeC
    Avatar
    Community Member
    5 Posts

    Returning Categories with at least one Article: better, prettier way? Link to this post

    Hi,

    I was wondering what would be the best way of getting all records of a model with at least one of an associated model, in a one-to-many relationship.

    In this particular example, I'm trying to get a list of all news categories that contain at least one article, filtering out empty categories.

    Could I streamline the following in any way using DataObject?

    DataObject::get("NewsCategory", "EXISTS (SELECT 1 FROM Article WHERE SiteTree_Live.ID = NewsCategoryID)")

    (NewsCategory extends Page, SiteTree)

    461 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.