Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Many-to-Many or Virtual Pages

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

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

Page: 1
Go to End
Author Topic: 2187 Views
  • txv11
    Avatar
    Community Member
    1 Post

    Many-to-Many or Virtual Pages Link to this post

    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.

  • ajshort
    Avatar
    Community Member
    244 Posts

    Re: Many-to-Many or Virtual Pages Link to this post

    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.

    2187 Views
Page: 1
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.