7911 Posts in 1354 Topics by 930 members
DataObjectManager Module
SilverStripe Forums » DataObjectManager Module » Access to $many_many Object(s) from Within $belongs_many_many Object(s)
Discuss the DataObjectManager module, and the related ImageGallery module.
Moderators: martimiz, UncleCheese, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w
|
Page:
1
|
Go to End | |
| Author | Topic: | 345 Views |
-
Access to $many_many Object(s) from Within $belongs_many_many Object(s)

11 April 2012 at 4:12am
Hi,
I have a group of pages -A- which has a $has_many relation to a set of DataObjects -B-.
I am in a nested loop showing all of the -B- objects which are associated with each -A-.
Now, -B- also has a $belongs_many_many relation to another page type -C- (which in turn has a $many_many relation to -B-).
Do I have access to the set of -C- objects associated with each -B- in the loop in any way? Would make sense that I would, I just can't figure out how I would refer to that in the template. I am trying to avoid writing a custom query and I can't pass an ID back to the PHP from the template.
-A-:
class Service extends Page {static $has_many = array(
"SubServices" => "SubService"
);-B-:
class SubService extends DataObject {
static $has_one = array(
"Service" => "Service"
);static $belongs_many_many = array(
"CaseStudy" => "CaseStudy"
);-C-:
class CaseStudy extends Page {
static $many_many = array(
"SubServices" => "SubService"
);Guidance would be appreciated. I've never had this exact kind of data relation before.
Thanks,
Garrett -
Re: Access to $many_many Object(s) from Within $belongs_many_many Object(s)

11 April 2012 at 7:01pm
On a service 'A'
<% control SubServices %>
In a 'B'
<% control CaseStudy %>
In a 'C'
<% end_control %>
<% end_control %>Or to you want to bypass the second control? In that case you'd need a custom query.
-
Re: Access to $many_many Object(s) from Within $belongs_many_many Object(s)

12 April 2012 at 1:28am
@Willr...... Once again...... you are my knight in shining armor!
This works brilliantly. I am now seeing all the Case Studies which are associated with a Service THROUGH a Subservice. Couldn't be happier.
What's funny is that this very approach was my instinct but there is something unintuitive about a control loop around a non-plural term, you know? I.e.: "CaseStudy" vs. "CaseStudies" as you would do in the outer loop. But I knew CaseStudies wouldn't work because there's no reference to that anywhere. Thank you so much, my good man. If I ever come to NZ you drink for free.
Now that I have this I will not require the solution to my other post from the same day but will file that snippet in my toolbox. Thanks for that too.
//Garrett
-
Re: Access to $many_many Object(s) from Within $belongs_many_many Object(s)

12 April 2012 at 6:52pm
What's funny is that this very approach was my instinct but there is something unintuitive about a control loop around a non-plural term, you know? I.e.: "CaseStudy" vs. "CaseStudies"
Well normally you name the relationship with a plural...
-B-:
class SubService extends DataObject {static $has_one = array(
"Service" => "Service"
);static $belongs_many_many = array(
"CaseStudies" => "CaseStudy" // <-- note case studies
);-C-:
class CaseStudy extends Page {static $many_many = array(
"SubServices" => "SubService"
);
| 345 Views | ||
|
Page:
1
|
Go to Top |


