PATH |
While anything that is not marked as a class property should be regarded simply as a database artifact (and not something to be accessed), you might need to read a non-class property value on rare occasions. One such example would be the need to obtain a foreign key for the generation of a qualifier. This discussion explains how to read a non-class property value, and provides an example of obtaining the primary key for an object when the object's primary key is not a class property.
Any attribute marked as a class property in EOModeler is included in a class definition when you generate source files for a class. In addition, any attributes marked as class properties are fetched and passed to your enterprise objects when you create instances from the database. Anything you mark as a class property should have a meaningful value in the object graph created when you fetch data from the database. Attributes that are essentially database artifacts you do not want to include in the user interface such as primary keys should not be marked as class properties. Anything you do not mark as a class property will not be part of your enterprise object, nor will it be inserted into an editing context, so you cannot use normal accessor methods to obtain their values.
However, you might need access to a primary or foreign key, such as when you are writing a restricting qualifier in single-table inheritance and you must use the value of the foreign key. How do you go about getting this information? How do you gain access to the raw data of the database before it gets pushed into the enterprise object.
EOUtilities provides a set of convenience methods for accessing raw rows of data, for accessing primary and foreign keys, and for accessing other attributes that are not class properties. (EOUtilities is an EOAccess class in Java and, in Objective-C, a category of methods on EOEditingContext.) The following EOUtilities methods are useful for accessing primary and foreign keys:
public static com.apple.yellow.foundation.NSDictionary primaryKeyForObject(EOEditingContext ec, EOEnterpriseObject eo); public static com.apple.yellow.foundation.NSDictionary destinationKeyForSourceObject(EOEditingContext ec, EOEnterpriseObject eo, String relationship);
The second method returns the foreign key for the rows at the destination entity of the specified relationship.
You can also access other items of data from the database that are not marked as class properties by using the following EOUtilities methods to fetch raw rows at the adaptor level.
public static com.apple.yellow.foundation.NSArray rawRowsWithQualifierFormat(com.apple.yellow.eocontrol.EOEditingContext ec, String entityName, String qualifierFormat, com.apple.yellow.foundation NSArray arguments); public static com.apple.yellow.foundation.NSArray rawRowsMatchingKeyAndValue (com.apple.yellow.eocontrol.EOEditingContext ec, String entityName, String key, Object value); public static com.apple.yellow.foundation.NSArray rawRowsMatchingValues (com.apple.yellow.eocontrol.EOEditingContext ec, String entityName, com.apple.yellow.foundation.NSDictionary matchingValues); public static com.apple.yellow.foundation.NSArray rawRowsForSQL (com.apple.yellow.eocontrol.EOEditingContext ec, String modelName, String sql); public static com.apple.yellow.foundation.NSArray rawRowsForStoredProcedureNamed ( com.apple.yellow.eocontrol.EOEditingContext ec, String procedureName, com.apple.yellow.foundation.NSDictionary arguments);
Here is a code example that gets a foreign key and uses it to qualify a fetchEOEditingContext ec; EOEnterpriseObject studio; NSDictionary foreignKeyToStudio; NSArray moviesMGM; ec = session().defaultEditingContext(); studio = EOUtilities.objectMatchingKeyAndValues(ec, "Studio", "name", "MGM"); // destinationKeyForSourceObject returns the foreign key in a // form that can be used to qualify the destination foreignKeyToStudio = EOUtilities.destinationKeyForSourceObject(ec. studio, "movies"); // You can use non-class properties to qualify a database fetch moviesMGM = EOUtilities.objectsMatchingValues(ec, "Movie", foreignKeyToStudio);