Question
Stephen Wilson · Mar 24, 2017

Can you create ad-hoc Caché relationship objects?

Can you create ad-hoc Caché relationship objects that are not part of the original Caché class definition? One of the drawbacks with relationships is that that the more relationships you have defined, the more difficult it can be manage. I was wondering rather than having all your relationships defined in the Caché class, could you programmatically create relationships between classes as and when they are required? We mostly use parent-child relationships in our classes. It is not uncommon for us to have 4 parent-child relationships defined in a single class and each of those children have relationships with other classes. Any thoughts?

0
0 319
Discussion (3)3
Log in or sign up to continue

I don't understand your question. Please elaborate on the following points:

  1. Do you want to create arbitrary relationships programmatically at design time (possible) or at runtime (not really)?
  2. What difficulties in managing relationships you have encountered?
  3. Can you provide some mock code demonstrating what you want to achieve?

1.  Good question but I'm not fully sure I understand the design-time and runtime concepts so I'll try to explain it in different terms. If you open two Caché  objects in memory using something like ##class(MyProjectName.MyClass1).%OpenId(id1) and ##class(MyProjectName.MyClass2).%OpenId(id2) can you create an arbitrary parent-child relationship object that links the two open objects within the executing ClassMethod? The implication is that if you were then to delete the parent object, you would also delete it's children.

2. Exporting individual classes from one server to another and getting compile errors due to missing dependencies and child dependencies. The classes have been provided by another programmer but the relationships are not necessary for what I am doing. I would question why so many relationships were created but I suspect the programmer was trying to ensure that all the data and all the levels of a single global were mapped to a class property.

3. I'll initially try to keep this high-level and theoretical rather than immediately delve into working code examples. I can tell you that the classes have been created from a Global using a CacheSQLStorage strategy.

It is possible to programmatically add a relationship to two persistent class definitions at runtime, and then compile those classes. That gives you the same result you'd get if you had added the relationship to the class definitions at design time. So I don't think that's what you want.

The term "Relationship" as defined in Caché means "objects of these classes can be linked at runtime, and this relationship will be stored when the objects are saved." So your need to create relationships between persistent objects "as and when they're required" doesn't really match up with this definition. Either a persistent class is in a relationship with another persistent class, or it isn't. It's not possible to have some objects of the class without the relationship definition, and other objects of the class with the relationship definition.

Maybe you just need to substitute one-many relationships for all of your parent-children relationships. One-many relationships are independent; the relationship is not required like it is in parent-children relationships. In v2013.1 and later, you can set the OnDelete action of the one-many relationship to "Cascade" so you get delete behavior similar to parent-children.