Access instance variables in Cache Objectscript

555 views Asked by At

I have a routine where I dynamically instantiate persistent objects using reflection. One of the properties of the object, which is also a persistent object, fails when I try to instantiate it using reflection ($Property). It fails because the property has been deleted from the database. Which makes sense but I can't seem to get the Id of that property (23). when I ZWrite the object I can see the property id in the print out.

+----------------- swizzled references ---------------
|      i%PropID = 23
|      r%PropID = ""

I have not been able to figure out how to get access to the i%ProdID value. If someone can help i would greatly appreciate it.

I'm using recursion to get an object’s properties and each propery’s subsequent properties (if they extents persistent) and so on, but in cases where the property is not actually persisted (somehow got deleted), i'd still like to get the id. I've thought of using sql but i want to keep away from that if at all possible.

3

There are 3 answers

0
wipallen On BEST ANSWER

The solution was to override the getter for that property object and is explained here:

https://docs.intersystems.com/latest/csp/docbook/DocBook.UI.Page.cls?KEY=GOBJ_propmethods

S getter = propertyName_"GetObjectId"
try {
    S id = $METHOD(object, getter)
} catch (ex) {}
return $G(id)
2
DAiMor On

i%<PropertyName> as well as r%<PropertyName> is just special syntax to get access to raw data of this property. If you want to get the value of this property you have to use original property name, PropID in your case.
To get the list of properties for a particular class, you can use classes %Dictionary.ClassDefinition and %Dictionary.PropertyDefinition, any way you may find interesting to read full definition of class programmatically and do whatever you want.

1
SSH On

You can use undocumented calls to get i% values, but even existence of i% properties is undocumented, as well as functions to access its values.

The best documented way to get ID of child object which was removed from database is GetStored call:

write ##class(parent.Class).ProdIDGetStored(ParentID) 

should return child ID from database (23 in your case)