Using Caché Objects - InterSystems Documentation
Using Caché Objects - InterSystems Documentation
Using Caché Objects - InterSystems Documentation
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Relationships<br />
Do company.Employees.Insert(emp)<br />
Write emp.TheCompany.Name<br />
// this will print out "Chiaroscuro LLC"<br />
You can load relationships from disk and use them as you would any other property. When<br />
you refer to a related object from the ONE side, the related object is automatically swizzled<br />
into memory in the same way as a reference (object-valued) property. When you refer to a<br />
related object from the MANY side, the related objects are not swizzled immediately; instead<br />
a transient %RelationshipObject collection object is created. As soon as any methods are called<br />
on this collection, it builds a list containing the ID values of the objects within the relationship.<br />
It is only when you refer to one of the objects within this collection that the actual related<br />
object is swizzled into memory.<br />
Here is an example that displays all Employee objects related to a specific Company:<br />
// open an instance of Company<br />
Set company = ##class(Company).%OpenId(id)<br />
// iterate over the employees; print their names<br />
Set key = ""<br />
Do {<br />
Set employee = company.Employees.GetNext(.key)<br />
If (employee '= "") {<br />
Write employee.Name,!<br />
}<br />
} While (key '= "")<br />
In this example, closing company removes the Company object and all of its related Employee<br />
objects from memory. Note, however, that every Employee object contained in the relationship<br />
will be swizzled into memory by the time the loop completes. To reduce the amount of<br />
memory that this operation uses—perhaps there are thousands of Employee objects—then<br />
modify the loop to “unswizzle” the Employee object after displaying the name, by calling<br />
the %UnSwizzleAt method:<br />
Do {<br />
Set employee = company.Employees.GetNext(.key)<br />
If (employee '= "") {<br />
Write employee.Name,!<br />
// remove employee from memory<br />
Do company.Employees.%UnSwizzleAt(key)<br />
}<br />
} While (key '= "")<br />
130 <strong>Using</strong> <strong>Caché</strong> <strong>Objects</strong>