25.01.2015 Views

Using Caché Objects - InterSystems Documentation

Using Caché Objects - InterSystems Documentation

Using Caché Objects - InterSystems Documentation

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The Persistence Interface<br />

Parameter READONLY = 1;<br />

This is only useful for cases where you have objects that are somehow mapped to preexisting<br />

storage (such as existing globals or an external database). If you call the %Save method on<br />

a READONLY object, it will always return an error code.<br />

12.2.2 Opening <strong>Objects</strong><br />

You can open (load an object instance from disk into memory) an object using the %OpenId<br />

method. The %OpenId method takes an ID value as input and returns a reference (OREF)<br />

to an in-memory object or a null value ("") if it cannot find or otherwise open the object.<br />

For example:<br />

// Open person "10"<br />

Set person = ##class(Sample.Person).%OpenId(10)<br />

Write "Person: ",person,!<br />

// should be an object reference<br />

// Open person "-10"<br />

Set person = ##class(Sample.Person).%OpenId(-10)<br />

Write "Person: ",person,!<br />

// should be a null string<br />

Note that in <strong>Caché</strong> Basic, the OpenId command is equivalent to the %OpenId method:<br />

person = OpenId Sample.Person(1)<br />

PrintLn "Name: " & person.Name<br />

The %OpenId method takes optional second and third arguments. The second argument is<br />

the concurrency level (locking) used to open the object (see below). The third object is a<br />

%Status value, passed by reference, that indicates whether the call succeeded or failed.<br />

The %OpenId method calls the callback method %OnOpen if it is present. Note that the<br />

%OnNew callback is not called when an object instance is opened from the database.<br />

12.2.2.1 Polymorphism and %OpenId<br />

The %OpenId method behaves polymorphically — that is, it may return different types of<br />

objects depending on the ID value it is passed.<br />

For example, the extent (set of) Sample.Person objects includes instances of Sample.Person<br />

as well as instances of Sample.Employee (a subclass of Sample.Person). Calling %OpenId<br />

for the Sample.Person class could return an instance of either Sample.Person or<br />

Sample.Employee depending on what is stored within the database:<br />

<strong>Using</strong> <strong>Caché</strong> <strong>Objects</strong> 103

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!