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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, Ed, biapar, Willr, Ingo, swaiba

Access to $many_many Object(s) from Within $belongs_many_many Object(s)


Go to End


4 Posts   1484 Views

Avatar
Garrett

Community Member, 245 Posts

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

Avatar
Willr

Forum Moderator, 5523 Posts

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.

Avatar
Garrett

Community Member, 245 Posts

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

Avatar
Willr

Forum Moderator, 5523 Posts

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"
);