Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

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

Returning Categories with at least one Article: better, prettier way?


Go to End


1048 Views

Avatar
gcoderre

Community Member, 5 Posts

4 February 2011 at 11:40am

Edited: 04/02/2011 11:43am

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)