Developer Forum »
Setting relations
11 posts

When a relation is set, is there an event in the partial class where I can do some extra logic? I'm trying to some aditional logic when a relation is set, and need to know which relation that was set etc.

181 posts

Hi!

For relations that you have created, there is a method called OnChangeBeforeUpdate on the class representing the relation that you can override to add your own logic. There are two different overloads of the method, one that is called when you can only select a single node, and one that is called when you can select multiple nodes.

The relation classes are located in the AqlRelations.cs file. Create a new .cs file in for the custom non-autogenerated code for the relation classes. I usually recommend also calling it AqlRelations.cs, but putting it in the WAF/Custom folder. 

The method overload for single values, have two parameters for the old and new value. The method for multiple values have a parameter called added that contains a list of the nodes added to the relation, and a parameter called removed that contains a list of the nodes removed from the relation.

There are also two methods that are invoked after that change has been saved to the database, called OnChangeAfterUpdate.

Let me know if you need more info.

An example of the partial class for the relation:

using WAF.Data.Query;
using WAF.Engine.Query;
using WAF.Engine.Query.Advanced;

namespace WAF.Engine.Content.MySite{

public partial class RelTestATestB: WAF.Engine.Content.RelationBase{

    public override void OnChangeBeforeUpdate(ContentBase content, Data.DataValue.NodeRelationDataValue dv, Common.ICKeyNxxC newValue, Common.ICKeyNxxC orgValue, Definition.MemDefProperty propDef, Definition.MemDefRelation relDef) {
        //add your custom logic here
        base.OnChangeBeforeUpdate(content, dv, newValue, orgValue, propDef, relDef);
    }

    public override void OnChangeAfterUpdate(ContentBase content, Data.DataValue.NodeRelationsDataValue dv, System.Collections.Generic.List<Common.ICKeyNxxC> added, System.Collections.Generic.List<Common.ICKeyNxxC> removed, Definition.MemDefProperty propDef, Definition.MemDefRelation relDef) {
        //add your custom logic here
        base.OnChangeAfterUpdate(content, dv, added, removed, propDef, relDef);
    }
}
}

                    
181 posts

I forgot to mention that if you want to do the same for relation built-in to the system, it is a bit more work. In the OnBeforeUpdate event in the content class, you have to manually check for differences between the stored data and the new object being stored. This is done by fetching another version of the node from the database, and comparing the database values with the updated object:

 

 public override void OnBeforeUpdate() {
         TestB oldValue = Session.GetContent<TestB>(this.NodeId);
         if (oldValue.TestA.GetId() == this.TestA.GetId()) { 
            
         }         
         base.OnBeforeUpdate(); 
     }
11 posts

Thanks a lot, I think this will solve my problems!

Will let you know if I'm facing any problems.

1