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

Archive /

Our old forums are still available as a read-only archive.

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

Many-to-Many or Virtual Pages

Go to End

2 Posts   3581 Views


Community Member, 1 Post

13 December 2008 at 4:26am

I'm new to Silverstripe, but very impressed with the simplicity of creating a template and modifying the underlying CMS output. What I'm now considering is how best to implement a product hierarchy in the system. We have products that fit into multiple categories and for SEO reasons I would like the same product pages to appear under different categories in the navigation system. It seems that virtual pages might help solve some of this and a many-to-many relationship might as well, but I'm not sure the best way of approaching it.

Here's the idea:

I have category A and B and product C. Product C fits in both categories. I want a sidebar navigation that lists product C in both categories depending on which one is currently being viewed and I want the same page (different URL) to appear under both categories. I also need a summary page (think ProductHolder) for both categories that pulls some summary data for each product in the category.

Do virtual pages have any use in this scenario or is a many-to-many relationship between categories and products the way to go (or some combination)? Thanks for any help.


Community Member, 244 Posts

13 December 2008 at 11:03am

Hey There,

I just recently finished a project with the exact same requirements. The way I set it up was to create a class "Product extends SiteTree" and "Product_Category extends SiteTree", and then I created a has_many/belongs_many_many relationship between the two.

You can then access this fairly simply in the template - if you have

class Product_Category extends SiteTree {
	public static $has_many = array ('Products' => 'Product');

you can just do something like
<% control ChildrenOf(ProductCategoryHolder) %>
	<a href="$Link">$Title</a>
	<% control Products %>
		<!-- each product will appear here for each category -->
	<% end_control %>
<% end_control %>

For administering the categories in the backend, I would recommend using a ManyManyComplexTableField on the Product and/or Category admin pages.