Search code examples
c#content-management-systemmany-to-manykentico

Many-to-many relationships in Kentico


Are there any best practices on how to build a Kentico CMS Portal implementation which is based around a many-to-many relationship (i.e. a website which sells food product and has a large section with recipes -- each product is used in many recipes, each recipe can use many of the products sold on the site)?

Is Kentico simply the wrong tool to do this or are there solutions within Kentico to handle this kind of relationships?


Solution

  • I would consider the built in Related Documents system. Under the properties tab on a document, there is a section for the related documents. The functionality is described here:

    Document Properties, Related Docs

    If this was a big piece of functionality for your website, you could probably add the section as another tab to the existing Page, Design, Form, Properties tabs for easier access. You would just need to modify the Modules area of Site Manager -> Development . Another heads up: they are technically unidirectional relationships, but you can structure your queries to go both ways:

    SELECT *
    FROM CMS_Document d
    JOIN CMS_Relationship r 
        ON (d.DocumentID = r.LeftNodeID
        OR d.DocumentID = r.RightNodeID)
    Where DocumentID = 100
    

    The above code will join the CMS_Document table to the CMS_Relationship table to give you all the related documents for Document with ID 100.

    If you take a peak at the database, the table structure for Related Documents is VERY simple:

    Related Document Tables

    So as McBeev suggested, I would create a custom document types for the products and the recipes. Then you can link them as I outlined above. Good luck!