Skip to main content

Connecting Objects

Now we can connect our new TicketItem object to a Show object and a TicketOrder. (Remember that when we defined TicketItem, we created properties with the same names—Show and TicketOrder—to hold these references.)

How do we make these connections? Simply by setting the property equal to the oref returned by the %New or %OpenID method that brought the object into memory.

We already have an oref for the TicketItem object we just created, but we don't have one for the Show object. What we do have is an object ID for the Show object, which was passed in as a parameter to the AddShow method. We can use the object ID in the %OpenId method to open the Show object and assign its oref to the shw variable.

 // Connect to a Show object
 Set shw = ##class(Cinema.Show).%OpenId(ShowID)
 Set itm.Show = shw
 // Connect to the Order object
 Set itm.TicketOrder = ord

Now, we can simply assign the oref values held in the shw and ord variables to the itm.Show and itm.TicketOrder properties.

—Utils.AddShow—
Utils.AddShow
ClassMethod AddShow(ShowID As %String)
{
    // Get an order object
    If $data(%session.Data("Order")) {
        Set ord = ##class(Cinema.TicketOrder).%OpenId(%session.Data("Order"))
    }
    Else {
        Set ord=##class(Cinema.TicketOrder).%New()
        // ...
    }

    // Create a new TicketItem object
    Set itm = ##class(Cinema.TicketItem).%New()

    // Connect to a Show object
    Set shw = ##class(Cinema.Show).%OpenId(ShowID)
    Set itm.Show = shw
    // Connect to the Order object
    Set itm.TicketOrder = ord

    // ...
    // Save incomplete order and remember its Id in %session.
    Do ord.%Save()
    Set %session.Data("Order") = ord.%Id()
}
FeedbackOpens in a new tab