Werner,

Good call indeed regarding the index rebuild! It causes some funny results
if you forget to do it :)

Updating object properties - typically this is the same as saving a new
object, but as you said if you have unique properties you can't just create
a new, identical object and overwrite the existing one, instead you must
open the existing one for edition:

&SQL(SELECT ID INTO :objID FROM MyClass WHERE MyUniqueProp = :PropVal)
If objID != ""
    Set myObj = ##class(MyClass).%OpenId( objID )
Else
    Set myObj = ##class(MyClass).%New()
// Common code
Set myObj.Prop1 = Val1
Set myObj.Prop2 = Val2
Set sc = myObj.%Save()

Note that the "hassle" comes when your unique property is not Cach�'s ID. If
it were, the SQL would not be necessary:

If ##class(MyClass).%ExistsId(myID)
    Set myObj = ##class(MyClass).%OpenId( objID )
Else
    Set myObj = ##class(MyClass).%New()

or even

Set myObj = ##class(MyClass).%OpenId( objID )
If '$IsObject(myObj)
    Set myObj = ##class(MyClass).%New()

There are ways around this though - search in the archive for a post about
unique indices by Paul Gausden. It involves writing a generator method...
The original post was written pre Cach� 5 and the generator syntax is quite
awkward, I don't know if Paul updated the document to v5.

HTH,

Ram�n



Reply via email to