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.

Data Model Questions /

Does anyone have some examples of DataObject::get and $many_many relationship


Reply


19 Posts   8186 Views

Avatar
mco

Community Member, 14 Posts

18 February 2010 at 12:28am

Edited: 18/02/2010 12:29am

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

Avatar
NickJacobs

Community Member, 144 Posts

18 February 2010 at 12:57pm

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 %>

Avatar
Hamish

Community Member, 712 Posts

18 February 2010 at 12:58pm

Edited: 18/02/2010 1:01pm

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.

Avatar
NickJacobs

Community Member, 144 Posts

18 February 2010 at 1:01pm

Edited: 18/02/2010 1:01pm

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

Avatar
NickJacobs

Community Member, 144 Posts

18 February 2010 at 1:18pm

so, could you also do this:

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

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

Avatar
Hamish

Community Member, 712 Posts

18 February 2010 at 1:23pm

Yep :)

Avatar
NickJacobs

Community Member, 144 Posts

18 February 2010 at 1:45pm

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?

Avatar
Hamish

Community Member, 712 Posts

18 February 2010 at 1:58pm

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 :/