Re: Finding the primary key value in an abstract EOGenericObject class

"Jerry W. Walker" <[email protected]>
Newsgroups gmane.comp.web.webobjects.devel
Message-ID <[email protected]>
Hi, Alan & Matt,

The methods that Alan is suggesting are both methods on  
ERXGenericRecord which is one of the Project Wonder extensions to  
EOF. These methods do not exist in WebObjects/EOF proper. However,  
even without using Project Wonder you can still get the primary key.

Presume these are defined:
     myEOGenericObject
     myEditingContext

Also, presume that your primary key is a simple key of (arbitrary)  
type PrimaryKeyValueClass.

If you want thePrimaryKeyValue, for myEOGenericObject which you've  
inserted into myEditingContext you would write:

   NSArray theKeys = myEditingContext.globalIDForObject 
(myEOGenericObject).keyValuesArray();

Then, if myEOGenericObject contains a simple primary key rather than  
a composite primary key, you could extract it with:

    PrimaryKeyValueClass thePrimaryKeyValue = (PrimaryKeyValueClass) 
theKeys.objectAtIndex(0);

or

     PrimaryKeyValueClass thePrimaryKeyValue = (PrimaryKeyValueClass) 
theKeys.lastObject();

With only one object in the array, the key is both the first and last  
object.  :-)


Alternatively, to obtain the primary key attributes as an  
NSDictionary rather than as an NSArray, you could write:

   NSDictionary theKeys = myEditingContext.globalIDForObject 
(myEOGenericObject).primaryKeyForGlobalID();

Then you could extract the key attribute or attributes from theKeys  
dictionary by name:

     PrimaryKeyValueClass thePrimaryKeyValue = (PrimaryKeyValueClass) 
theKeys.valueForKey(myKeysName);

Regards,
Jerry


On Jan 28, 2006, at 4:51 PM, Alan Ward wrote:
> primaryKey() returns it as a String, rawPrimaryKey() returns it as  
> whatever type it is.
>
> On Jan 28, 2006, at 1:22 PM, Matt Kime wrote:
>> Hello,
>>
>> Does anyone know how I can find the primary key value of any given
>> EOGenericObject class?I'm attempting to create an abstract class that
>> use to implement EOEnterpriseObjects with history features.
>>
>> many thanks,
>> matt


--
__ Jerry W. Walker,
    WebObjects Developer/Instructor for High Performance Industrial  
Strength Internet Enabled Systems

     [email protected]
     203 278-4085        office
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.