3069 Posts in 868 Topics by 650 members
| Go to End | Next > | |
| Author | Topic: | 6380 Views |
-
Does anyone have some examples of DataObject::get and $many_many relationship

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
-
Re: Does anyone have some examples of DataObject::get and $many_many relationship

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. -
Re: Does anyone have some examples of DataObject::get and $many_many relationship

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
-
Re: Does anyone have some examples of DataObject::get and $many_many relationship

7 August 2009 at 8:52am Last edited: 7 August 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 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%> -
Re: Does anyone have some examples of DataObject::get and $many_many relationship

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
-
Re: Does anyone have some examples of DataObject::get and $many_many relationship

10 August 2009 at 7:05am
Thanks Hamish! (I've been following the thread in the dark…)
-
Re: Does anyone have some examples of DataObject::get and $many_many relationship

10 August 2009 at 10:05am
no prob
-
Re: Does anyone have some examples of DataObject::get and $many_many relationship

26 November 2009 at 3:33am Last edited: 26 November 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.
| 6380 Views | ||
| Go to Top | Next > |



