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


Go to End
Reply


19 Posts   8091 Views

Avatar
robinp

Community Member, 33 Posts

3 August 2009 at 6:02pm

Hi,

I'm have a problems getting my DataObject::get query's to work with $many_many relationships. Does any one have any example code ?

Is this as case where you need to you using joints raw SQL ? or is because I need to write getter and getters ?

Cheers

Robin

Avatar
Hamish

Community Member, 712 Posts

6 August 2009 at 4:43pm

What are you trying to do? If you want to filter an object on fields in a many_many relation, you might need to use joins.

For example:

return DataObject::get(
   "Member",
   "`Group`.`Title` = 'Users'",
   "",
   "LEFT JOIN `Group_Members` ON `Group_Members`.`MemberID` = `Member`.`ID`
      LEFT JOIN `GROUP` ON `GROUP`.`ID` = `Group_Members`.`GroupID`"
); // returns Members in the group called 'Users'

You can also use an objects relationship getter with a filter, but this returns the other side of the relationship:

return $group->Members("Email LIKE '%@gmail.com'");
// returns members in the group that have an email hosted with gmail.

Avatar
robinp

Community Member, 33 Posts

6 August 2009 at 11:21pm

Hi Hamish,

Thank you. The was exactly the sort thing I was after. I got it working with get:: but I interested in the get solution as well. This would like some think like

function getGmailMember() {
return $group->Members("Email LIKE '%@gmail.com'");
}

Is that right ? and it could be used like $this->GmailMember ? or $GmailMember in the templates ?

Cheers

Robin

Avatar
Hamish

Community Member, 712 Posts

7 August 2009 at 8:52am

Edited: 07/08/2009 8:56am

Well, $group would have to be defined first. If this function was within Group.php it would be:

function getGmailMember() {
   return $this->Members("Email LIKE '%@gmail.com'");
}

($this refers to the current object).

Rather than modifying group, you could apply a [url=http://doc.silverstripe.org/doku.php?id=dataobjectdecorator]DataObjectDecorator[/url] to the group object, in which case the function in the decorator would be:

function getGmailMember() {
   return $this->owner->Members("Email LIKE '%@gmail.com'");
}

($this->owner refers to the object that the decorator is applied to)

Templates

Since this will return a DataObjectSet, you would probably refer to it like this in your template:

<% if GmailMember %>
   <% control GmailMember %>
      $Name.XML
   <% end_control %>
<% else %>
   No members found!
<% end_if%>

Avatar
robinp

Community Member, 33 Posts

9 August 2009 at 9:19pm

HI Hamish,

Thank you for that. I've got it working.

When I read my message again after your reply I realized trying to use $GmailMember was quite dumb. Thank you for being patience with me.

Cheers

Robin

Avatar
Juanitou

Community Member, 323 Posts

10 August 2009 at 7:05am

Thanks Hamish! (I've been following the thread in the dark…)

Avatar
Hamish

Community Member, 712 Posts

10 August 2009 at 10:05am

no prob

Avatar
baba-papa

Community Member, 279 Posts

26 November 2009 at 3:33am

Edited: 26/11/2009 3:34am

Hello Hamish,

many thanks for your "LEFT JOIN" code example. Finally I understood how to filter DataObjects by values of a many-many relation.
It would be great if this could find it´s way to the documentation of SS.

Go to Top