Documentation Archive Developer
Search
PATH  WebObjects 4.0 Documentation > EOF Tools and Techniques

Table of Contents Previous Section

Flattened Attributes

A flattened attribute is a special kind of attribute that you effectively add from one entity to another by traversing a relationship. When you form a to-one relationship between two tables (such as MovieRole and Talent), you can add attributes from the destination entity to the source entity-for example, you can add a lastName attribute to MovieRole to identify the actor who played a particular role. This is called "flattening" an attribute. Flattening an attribute is equivalent to creating a joined column; it allows you to create objects that extend across tables.

When Should You Use Flattened Attributes?

Flattening attributes is just one way to conceptually "add" an attribute from one entity to another. A generally better approach is to traverse the object graph directly through relationships. Both WebObjects Builder and Interface Builder make this easy by supporting the notion of key paths. You can also access the values in other objects programmatically through relationship references, as described in the chapter "Designing Enterprise Objects" in the book Enterprise Objects Framework Developer's Guide.

The difference between flattening attributes and traversing the object graph (either programmatically or by using key paths) is that the values of flattened attributes are tied to the database rather than the object graph. If an enterprise object in the object graph changes (for example, because a user changed a value in another part of the application), a flattened attribute can quickly get out of sync. For example, suppose that you flatten a deptName attribute into an Employee object. If a user then changes the employee's department reference to a different department or changes the name of the department itself, the flattened attribute won't reflect the change until the changes in the object graph are committed to the database and the data is refetched. However, if you're using key paths in this scenario, a user of your application sees changes to data as soon as they happen in the object graph. This ensures that your application's view of the data remains internally consistent.

Therefore, you should only use flattened attributes in the following cases:

Flattening an Attribute

To flatten an attribute, the relationship you traverse must be a to-one relationship.

To flatten an attribute:

  1. In the browser mode of the Model Editor, select the relationship that gives you access to the attribute you want to add to your entity (you don't have to use browser mode, it just makes it easier to see the results of the operation).

    For example, to add the name of an actor to MovieRole, you can traverse a talent relationship (which represents MovieRole's relationship to Talent) and add the actor's last name (lastName) to MovieRole as a flattened attribute. This is a contrived example, because in this case it would be better to use a key path than to flatten an attribute.

  2. Select the attribute you want to add (lastName), and choose Property Flatten Property.

    Figure 30. Adding a Flattened Attribute

    Alternatively, you can use the button in the toolbar.

    The derived attribute (in this example, talent_lastName) appears in the list of properties for MovieRole. The format of the name reflects the traversal path: the attribute lastName is added to MovieRole by traversing the talent relationship.

  3. Examine the derived attribute (talent_lastName) in the Attribute Inspector.

    Figure 31. Examining a Flattened Attribute in the Attribute Inspector

    In the Attribute Inspector, the pop-up list to the left of the Definition field identifies the attribute as "Derived".

  4. Edit the Name text field to simplify the attribute name (for example, to "lastName").

    The Definition field (the second text field from the top of the Attribute Inspector) must accurately reflect the attribute's external name and the table in which it resides. For example, if you edit its text to be "Name" and change its mode to "Column," it no longer maps to an existing attribute. In other words, only edit this field if you are sure you can predict the outcome.

    To display this flattened attribute, use the Data Browser.

  5. Select the flattened attribute along with another "native" attribute for verification purposes.

    To select multiple, non-contiguous attributes in the Model Editor on Windows NT, hold down the Control key while you click on each attribute. On Rhapsody, use the Shift key.

  6. View the attributes in the Data Browser as shown in Figure 32

    Figure 32. Using the Data Browser to Check Your Model

Displaying data associated with your model in the Data Browser is a good way to check that the model is synchronized with the database. If your model is out of sync with the database (for example, if you try to implement a relationship for which there is no corresponding relationship in the database), attempting to display data in the Browser will fail. For more discussion of the Data Browser, see the chapter Interacting with a Database.

Table of Contents Next Section