Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Hazar Askari 12 posts 73 karma points
    Jan 08, 2015 @ 10:45
    Hazar Askari
    0

    How does relations work?

    Hello. I have set up nuPickers for a test project and it seems great! 

    My test appliucation is following. I have Students and Classes. Students are linked/related to classes and i assume that classes will be automagically linked/related to the students right? how do i get these relations from my classes View? i have managed to get the relations from the Students view. So now i can see each classes that the students are taking, but is it possible to go to the Classes view and get all the students linked to this class? How do i do it? do i need to setup a property (i havent setup any property for this yet). how do i do this with code example?

  • John C Scott 473 posts 1183 karma points
    Jan 08, 2015 @ 12:37
    John C Scott
    0

    In this example I create some content where I had X & Y nodes under a Z node, and I wanted to allow the user to pick an X that was related from the Y.

    I created a relation in the Developers section under Relation types:

    name: x2y
    alias: x2y
    direction: Bidirectional
    parent: Document
    child: Document 

    then in the data types added an editor

    property editor: nuPickers:Xml Checkbox Picker
    xml data: Content
    xpath: //Z/X [@isDoc]   //this is to only allow X nodes to be selected which are under the Z node in the root
    relation mapping: x2y   //this is the one created above
    save format: Relations Only 

     added this property to my document type for Y and added some content which looks like this:

    So then this is the bit I absolutely love about nuPickers, first I went round the houses looking at the various relations and relation service documentation and got nowhere for a while. nuPickers is excellently documented here https://github.com/uComponents/nuPickers/wiki/Property-Value-Converter you can see the that an extension is included that looks up and manages the picker data for you and gets you all the items in an easy to use list. So the template code I have for Y is very simple:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using nuPickers;
    @{
        Layout = null;
    }
    
    <h1>A list of the Xs related to this Y</h1>
        @{
            var pickedItems = CurrentPage.relation;
            <ul>
            @foreach (var item in pickedItems.PickedKeys)
            {
                var x = @Umbraco.Content(item);
                <li><a href="@x.Url">@x.Name</a></li>
            }
        </ul>    
         }

    and so then we see the output looks like this:

    A list of the Xs related to this Y


     

     

  • John C Scott 473 posts 1183 karma points
    Jan 08, 2015 @ 12:48
    John C Scott
    0

    However - I do see that from reading your question again - I think you had this part working. What you'd like to do is list it the other way around? As in listing all the Ys that picked this X on the "X" page. I'll take a look, I don't think this is supported by the helper in nuPickers but I'll take a look.

  • Hazar Askari 12 posts 73 karma points
    Jan 08, 2015 @ 16:46
    Hazar Askari
    0

    Exactly what you said in your last post John. I wanted to see how i can fetch it the other way around. The example you showed is well documented, and i managed to do that, its the other way around im interested in. Ill be eagerly waiting for your solution :)

  • John C Scott 473 posts 1183 karma points
    Jan 08, 2015 @ 18:48
    John C Scott
    0

    Well I'm not totally happy with this answer, but with a lot of messing about I got to this:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @{
        Layout = null;
        var relations = ApplicationContext.Services.RelationService.GetByParentId(CurrentPage.Id);
    }
    <h1>A list of the Ys that chose this X</h1>
    <ul>
    @foreach (var r in relations)
    {
        if (r.RelationTypeId ==2)
        {
            var x = Umbraco.Content(r.ChildId);
            <li><a href="@x.Url">@x.Name</a></li>
        }
    }
    </ul>

    The problems with this are that if there are no relations with that type ID you'll get an empty <UL> but if I add a query .where(r => r.RelationTypeId = 2) as shown documentation then I get an error "Cannot use a lambda expression as an argument to a dynamically dispatched operation" I'm not really sure what this means. Also was surprised by the fact that the one that chose is the child and the one selected is the parent, which seems backwards. Anyway this works at least... and I get this output:

    A list of the Ys that chose this X

    :)

     

  • Hazar Askari 12 posts 73 karma points
    Jan 08, 2015 @ 19:08
    Hazar Askari
    0

    Nice! can i ask what the if (r.RelationTypeId ==2) is? whats that 2? how robust is this way of doing it? does it require any kind of node structure or anything?

  • Hazar Askari 12 posts 73 karma points
    Jan 08, 2015 @ 19:08
    Hazar Askari
    0

    Actually, can you upload your code somewhere so i can look at it ?

  • John C Scott 473 posts 1183 karma points
    Jan 09, 2015 @ 10:59
    John C Scott
    0

    >can i ask what the if (r.RelationTypeId ==2) is?

    the relation type ID comes from here:

    So it's the ID of the relation type we created, it doesn't feel outrageous to hard-code it as it is the specific relation type to the template you're building, you could possible get it by alias instead - so you could have something like:

    int rtID = ApplicationContext.Services.RelationService.GetRelationTypeByAlias("x2y").Id;

    In theory this is better, as if you transfer the site by courier the ID could change but the alias would stay the same. All this is only needed anyway if you have more than one relation type in use, you don't really need to filter it if you have only one. You could in theory also get the name of the relation type from the picker but the picker is on another page anyway so it would be another  arbitrary lookup.

    I really would much prefer to write something like:

    var relations =ApplicationContext.Services.RelationService.GetByParentId(CurrentPage.Id).Where(r => r.RelationTypeId = 2);

    but I don't understand why this gives me an error - I might ask a question in another thread about this and if I get an answer I'll post a link here.

    If you had that then you could wrap the UL with something like 

    if (relations.Count >0) 

    which would be better, you could then maybe have an else that added a message like "this Y has been selected for no Xs" Linq is great for this sort of thing but I don't really understand why it doesn't work.

    I'll upload my solution and put a link here in a bit for you.

     

     

  • John C Scott 473 posts 1183 karma points
    Jan 09, 2015 @ 11:18
    John C Scott
    0

    I can't seem to access any filesharing services from behind the firewall here. If you send me an email to [email protected] then I can email a copy of the project to you.

  • Daniel Bardi 927 posts 2562 karma points
    Feb 27, 2015 @ 01:16
    Daniel Bardi
    0

    nuPicker handles the relations under the hood, so you shouldn't need to go into the relations table.  Simple use the nuPicker property value convertor retrieve values on both ends.

Please Sign in or register to post replies

Write your reply to:

Draft