Jump to:

3431 Posts in 1058 Topics by 734 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: 7765 Views
  • robinp
    Avatar
    Community Member
    33 Posts

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

    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

  • Hamish
    Avatar
    Community Member
    712 Posts

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

    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.

  • robinp
    Avatar
    Community Member
    33 Posts

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

    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

  • Hamish
    Avatar
    Community Member
    712 Posts

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

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

  • robinp
    Avatar
    Community Member
    33 Posts

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

    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

  • Juanitou
    Avatar
    Community Member
    323 Posts

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

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

  • Hamish
    Avatar
    Community Member
    712 Posts
  • baba-papa
    Avatar
    Community Member
    279 Posts

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

    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.

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