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.

Archive /

Our old forums are still available as a read-only archive.

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

How to use ComponentSet?


Go to End


3 Posts   3873 Views

Avatar
Cy

Community Member, 11 Posts

24 October 2008 at 10:53am

Edited: 24/10/2008 11:12am

Books can have many categories. Categories can have many books.

I'd like to return a list of books from a specific category.

I have Book.php (extends Page), Category.php (extends DataObject).

BookHolder.php Controller

function getBooksByCategory($categoryID){
  $whereStatement = "CategoryID ='". $categoryID ."'";
  return DataObject::get("Books", $whereStatement); // Do I replace DataObject with ComponentSet?
}

The ComponentSet page in the documentation doesn't explain much.

I read that ComponentSet has to do with Many_Many relationships.

Any examples are greatly appreciated!

Thanks!

Avatar
Cy

Community Member, 11 Posts

24 October 2008 at 6:51pm

This did the trick

function getBooksByCategory($categoryID){
$ByCategoryID = DataObject::get_one('Category', 'ID="'.$categoryID.'"');
if($ByCategoryID) {
return $ByCategoryID->getManyManyComponents('Books');
}

Avatar
Phalkunz

Community Member, 69 Posts

27 October 2008 at 1:44pm

Edited: 27/10/2008 1:45pm

This is much cleaner

return $ByCategoryID->Books();