25.01.2015 Views

Using Caché Objects - InterSystems Documentation

Using Caché Objects - InterSystems Documentation

Using Caché Objects - InterSystems Documentation

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

An Invoice class<br />

class MyApp.Invoice Extends %Persistent [ClassType = persistent]<br />

{<br />

Property CustomerName As %String;<br />

/// an Invoice has CHILDREN that are LineItems<br />

Relationship Items As LineItem [inverse = TheInvoice, cardinality = CHILDREN];<br />

}<br />

and LineItem:<br />

/// A LineItem class<br />

class MyApp.LineItem Extends %Persistent [ClassType = persistent]<br />

{<br />

Property Product As %String;<br />

Property Quantity As %Integer;<br />

In-Memory Behavior of Relationships<br />

/// a LineItem has a PARENT that is an Invoice<br />

Relationship TheInvoice As Invoice [inverse = Items, cardinality = PARENT];<br />

}<br />

14.3 In-Memory Behavior of Relationships<br />

Programmatically, relationships behave as properties. Single-valued relationships (cardinality<br />

of “ONE” or “PARENT” ) behave like atomic (non-collection) reference properties. Multivalued<br />

relationships (cardinality of “MANY” or “CHILDREN” ) are instances of the<br />

%RelationshipObject class which has a collection-like interface.<br />

For example, you could use the Company and Employee objects defined above in the following<br />

way:<br />

// create a new instance of Company<br />

Set company = ##class(Company).%New()<br />

Set company.Name = "Chiaroscuro LLC"<br />

// create a new instance of Employee<br />

Set emp = ##class(Employee).%New()<br />

Set emp.Name = "Weiss,Melanie"<br />

Set emp.Title = "CEO"<br />

// Now associate Employee with Company<br />

Set emp.TheCompany = company<br />

// Save the Company (this will save emp as well)<br />

Do company.%Save()<br />

// Close the newly created objects<br />

Do company.%Close()<br />

Do employee.%Close()<br />

Relationships are fully bi-directional in memory; any operation on one side is immediately<br />

visible on the other side. Hence, the code above is equivalent to the following, which instead<br />

operates on the company:<br />

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

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

Saved successfully!

Ooh no, something went wrong!