Search code examples
kentico

Kentico Module Parent/Child Relationship - Using GUID instead of ID for parent reference


I set up a custom module within Kentico and then followed this to create parent/child relationships between the classes in the module. That works great and I have everything working using:

ChildParentID = {% ToInt(UIContext.ParentObjectID) %}

However, I want to use the parent's GUID as the foreign key, not their ID. I am able to configure the info and info provider classes for the child to specify the GUID of the parent as the foreign key but I can't figure out how to actually get the parent GUID when retrieving the child classes. I was hoping Kentico would provide an easy macro like:

ChildParentGUID = {% ToInt(UIContext.ParentObjectGUID) %}

but that doesn't exist and I'm not sure how to get the GUID of the parent in the macro. Any help would be appreciated, thanks.


Solution

  • First, I suggest you to reconsider to use ID as a parent reference. It will simplify other things for you automatically such as automatic deletion of children, proper inclusion within import/export/staging, etc.

    You probably just don't have TypeInfo of those two classes set up properly.

    Most typical mistake is that you list parent ID both as parentIdColumn in constructor, and in ObjectDependencies which is then causing unwanted double translation.

    Can you post here your TypeInfo configurations for both classes?

    You can also check my article to try out a specific scenario how to set it up here

    If you insist on using GUID for parent relation and it works for you, you can use the following to get related parent GUID:

    {% UIContext.EditedObjectParent.GUID %}