Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » How to use ComponentSet?

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

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

Page: 1
Go to End
Author Topic: 2882 Views
  • Cy
    Avatar
    Community Member
    11 Posts

    How to use ComponentSet? Link to this post

    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!

  • Cy
    Avatar
    Community Member
    11 Posts

    Re: How to use ComponentSet? Link to this post

    This did the trick

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

  • Phalkunz
    Avatar
    69 Posts

    Re: How to use ComponentSet? Link to this post

    This is much cleaner

    return $ByCategoryID->Books();

    2882 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.