Copied to clipboard

Flag this post as spam?

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


  • Matt Brailsford 2958 posts 15629 karma points MVP 7x c-trib
    Apr 18, 2010 @ 11:53
    Matt Brailsford
    0

    Auto cleanup relationships?

    Hi,

    I'm not sure if this is a bug or not but I've noticed when using the relationship API, if either or both related nodes are deleted, the relationship is still left in the relationship table.

    I was wondering what is the best way to auto cleanup any orphaned relationships? I was thinking about hooking up to the afterdelete events, but I'm not sure what node types can have relationships defined against them. Anybody got any more info?

    Many thanks

    Matt

  • Matt Brailsford 2958 posts 15629 karma points MVP 7x c-trib
    Apr 18, 2010 @ 18:46
    Matt Brailsford
    1

    Ok, had a crack at the code myself, but really not sure why it's not working. I have a relationship setup between a member and a document, I then have the following setup to detect the deletion of a node and to remove any relationships defined on that node:

    public

     

    class RelationshipAppBase : ApplicationBase

    {

     

    public RelationshipAppBase()

    {

     

    CMSNode.AfterDelete += new EventHandler<DeleteEventArgs>(CMSNode_AfterDelete);

    }

     

    protected void CMSNode_AfterDelete(object sender, DeleteEventArgs e)

    {

     

    // Cleanup orphan relationships

     

    var node = sender as CMSNode;

     

    if (node == null)

     

    return;

     

    var relations = Relation.GetRelations(node.Id);

     

    foreach (var relation in relations)

    {

    relation.Delete();

    }

    }

    }

    Debugging, it hits the event handler, but the call to Relation.GetRelations always returns an empty array. I've also tried it from both ends (ie delete the member first, or delete the document first) but the list always seems to be empty. Anybody got any ideas why this might be?

    Many thanks

    Matt

  • Matt Brailsford 2958 posts 15629 karma points MVP 7x c-trib
    Apr 18, 2010 @ 19:11
    Matt Brailsford
    0

    Ahhhhh. Worked it out. My problem was I didn't have a RelationType defined in the DB. Guess I got a little confused with the documentation I was following as nowhere seems to say you need to setup your relationTypes first. After installing the relation type package and defining the my relations, it all worked fine.

    Matt

  • Matt Brailsford 2958 posts 15629 karma points MVP 7x c-trib
    Apr 18, 2010 @ 19:31
    Matt Brailsford
    0

    In fact, now that I've defined the relationship type, it does actualy clean up after itself. Maybe there needs to be something to prevent a relationship from being created with an invalid relationship type?

    Matt

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies