Skip to main content

Obtaining a TicketOrder Object 2

Let's examine this code a little more closely.

First, we use the ObjectScript $Data function to determine whether a value for Order has been previously saved in the %session object:

 If $data(%session.Data("Order")) {
 }

If not, we create a new TicketOrder object by calling the %New method.

 Set ord=##class(Cinema.TicketOrder).%New()

The %New method creates a new object in memory, fills in any default property values specified in the class definition, and returns an object reference (oref) which we assign to the local variable ord. This oref serves as a “handle” that we use to access the in-memory object.

Normally, from one browser request to the next, the only thing that Caché “remembers” is data that we have explicitly stored in the %session object. This is consistent with the “stateless” nature of the Web.

We want to remember this object for subsequent use, so we store it in the database using the %Save method (remember that TicketOrder is a persistent object) and place its object ID in the %session object. Later, if we detect that a TicketOrder object already exists (i.e., that an Object ID is saved in the %session object,) we can retrieve it using the Data property of the %session object.

We place the Object ID in the %session object at the end of the method. Note that a newly created object does not have an Object ID until it has been saved to the database with the %Save method.

—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()
        // ...
    }

    // ...

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