Many-to-Many page relationships within the admin interface

Nov 11, 2008 at 6:24 PM
Hi there

I need to create a many-to-many relationship between page types in N2.

For example, if I have 2 page types: "books" and "topics" I need some way in the edit interface to specify a list of topics that a given book covers. Similarly (though in my particular case less importantly) I would like to specify a list of books that cover a given topic.

I've had a look through the provided controls and have not found anythign that obviously fits my needs. I can imagine some kind of checkbox control that is associated with a given ContentItem tyoe would be ideal.

I'm more than happy to attempt to implement this functionality (either independently or as a patch) if it does not already exist, but could do with some pointers on where to start!

Kind regards

Graham
Nov 11, 2008 at 7:26 PM
Try creating a new implementation AbstractEditableAttribute and render a checkbox with the current topics.
Coordinator
Nov 11, 2008 at 8:19 PM
If you create an editable as theonlylawislove suggests you can store the links in the content item using the detail collection feature (in the UpdateItem method):

// will create or replace any existing topics on the book with the new set
IList<ContentItem> selectedTopics = GetSelectedTopics(mycheckboxlist);
bookItem.GetDetailCollection("Topics", true).Replace(selectedTopics);
Nov 12, 2008 at 2:35 PM
Hi all

This worked great - thanks very much!
Apr 30, 2009 at 11:12 AM
Is this how N2 implements many to many relationships with items? I'm having a hard time getting to grips with how I can do queries to check on relationships in N2!
Coordinator
May 4, 2009 at 10:35 PM
Take a look here http://code.google.com/p/n2cms/source/browse/trunk/src/N2.Tests/Persistence/NH/ItemFinderTests.cs

        [Test]
        public void LinkInDetailCollection()
        {
            IList<ContentItem> items = finder.Where.Detail("DetailCollection").Eq(startPage).Select();
            Assert.AreEqual(3, items.Count);
        }

Keep in mind that the finder fluent interface is an abstraction over a generic purpose item->detail model. The problems you want to solve might be best solved closer to the database.