Jump to:

3372 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Does anyone have some examples of DataObject::get and $many_many relationship

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

Page: 1 2 3
Go to End
Author Topic: 7415 Views
  • mco
    Avatar
    Community Member
    14 Posts

    Re: Does anyone have some examples of DataObject::get and $many_many relationship Link to this post

    The LEFT JOIN works perfect, however, Sorting doesn'twork for me...

    I'm using following get:

    $fieldset = DataObject::get(
             "Article",
             "`Category`.`Title` = '$category'",
             "`Category`.`Order`",
             "LEFT JOIN `Article_Categories` ON `Article_Categories`.`ArticleID` = `Article`.`ID`
              LEFT JOIN `Category` ON `Category`.`ID` = `Article_Categories`.`CategoryID`",
              ""
          );

    `Category`.`Order` is a integer column...

    On the other hand the following SQL Statement works just fine and sorts the output:

    SELECT * FROM `Article`
    LEFT JOIN `Article_Categories` ON `Article_Categories`.`ArticleID` = `Article`.`ID`
              LEFT JOIN `Category` ON `Category`.`ID` = `Article_Categories`.`CategoryID`
    ORDER BY `Category`.`Order` ASC

  • NickJacobs
    Avatar
    Community Member
    144 Posts

    Re: Does anyone have some examples of DataObject::get and $many_many relationship Link to this post

    Another many_many question...can anyone tell me why this works:
    ( a Banner has many_many Pages)

    function sectionBanners(){
    ....
    $theSection = DataObject::get_by_id('SiteTree', $sectionID);
    return $theSection ;
    ]

    and

    <% control SectionBanners %>
    <% control Banners %>
    $Image.URL
    <% end_control %>
    <% end_control %>

    but this doesn't:

    function sectionBanners(){
    ....
    $theSection = DataObject::get_by_id('SiteTree', $sectionID);
    return $theSection->Banners;
    ]

    <% control SectionBanners %>
    $Image.URL
    <% end_control %>

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Does anyone have some examples of DataObject::get and $many_many relationship Link to this post

    Change:

    return $theSection->Banners;

    to:

    return $theSection->Banners();

    The difference is the first example searches for the property Banners, or a method called getBanners(), whereas the second will call the relation Banners and return the correct Component Set.

  • NickJacobs
    Avatar
    Community Member
    144 Posts

    Re: Does anyone have some examples of DataObject::get and $many_many relationship Link to this post

    yep...that works! thanks Hamish...I did try that initially, but must have had a typo

  • NickJacobs
    Avatar
    Community Member
    144 Posts

    Re: Does anyone have some examples of DataObject::get and $many_many relationship Link to this post

    so, could you also do this:

    return $theSection->Banners("BannerType = '$bannerType'");

    - the Banner DataObject has a 'BannerType' => 'Text'

  • Hamish
    Avatar
    Community Member
    712 Posts
  • NickJacobs
    Avatar
    Community Member
    144 Posts

    Re: Does anyone have some examples of DataObject::get and $many_many relationship Link to this post

    and.... can I test that the resulting dataset has anything in it??

    kinda like

    if($theSection->Banners("BannerType = '$bannerType'")){
                return $theSection->Banners("BannerType = '$bannerType'");
                } else {
                return DataObject::get('Banner');
             }

    this doesn't seem to be working, but is there any other way of testing for an empty result set?

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Does anyone have some examples of DataObject::get and $many_many relationship Link to this post

    Relation getters will return an empty ComponentSet if it doesn't find anything. This is different from DataObject::get() which returns false, so you'll need to modify your function slightly, eg:

    function someMethodName() {
       ...
       $bannerSet = $theSection->Banners("BannerType = '$bannerType'");
       // Only return $bannerSet if it has items, otherwise get all Banner objects:
       return ($bannerSet->Count()) ? $bannerSet : DataObject::get('Banner');
    }

    Side note: It bugs me that DataObject::get() and relation getters behave differently. Ideally DataObject::get() would returned empty DataObjectSets rather than false, so you didn't have to check for it's existance all the time and everyone would be used to testing Count() or Exists(), but that's just the way it is :/

    7415 Views
Page: 1 2 3
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.