Setting an attribute based on PK?

Ashley Aitken <[email protected]> Sun, 25 Apr 2004 16:16:55 +0800
Newsgroups gmane.comp.web.webobjects.eof
Message-ID <[email protected]>
Howdy All,

I want to set an attribute (not the primary key) based on the 
primaryKey value.  But, of course, the primary key is not generated 
until during the initial saveChanges.

Here's what I tried to do:

Briefly, I added a custom primary key generation delegate that calls 
the standard primary key generation method but then also calls a method 
to set the other attribute.

All this seems to work fine - well all the code gets called - I see the 
primary key being generated on save, I see the original value of the 
other attribute as expected, I see the updated value as expected.

The only thing is, it still saves the original value of the other 
attribute.  I seem to remember it is ill-advised to change an EO during 
validation (although this is not validation, probably later) maybe this 
is similar.

My thinking is that the operations would happen in this order:
saveChanges
     validation (I have none)
     primaryKey generation + changing other attribute (using 
takeStoredValueForKey)
     generating and executing SQL to save new row

But, as I mentioned, I can't seem to make my changes to the other 
attribute get used in the actual save (the changes occur - I can see 
them with storedValueForKey - but are not written to the database).

Any ideas?  If not what is wrong with this, how better to do what I am 
trying to do.

Thanks in advance,
Ashley.

PS Why do I want to do this?

The entity has an entityCode (not the primary key) but used by the user 
to refer to items (so it should be unique, but not strictly so).  I 
give the user an opportunity to set this entityCode (in fact, I require 
it) but if they set it to "*" then it should be auto-generated.  To do 
that, I am, at present, just trying to give it the value of the primary 
key (an integer to string), but later I may want to combine it will 
some other attributes to make a more meaningful entityCode (eg WOT-12).


--
Ashley Aitken
Perth, Western Australia
mrhatken at mac dot com