Re: New Enterprise-Object of old one

David LeBer <[email protected]> Thu, 12 May 2011 14:52:12 -0400
Newsgroups gmane.comp.web.webobjects.woproject.devel
Message-ID <[email protected]>
On 2011-05-12, at 2:35 PM, Peter Müller wrote:

> Hello WO-Checkers,
> is there an easy way to make a copy on Enterprise-Object and save this as a "new" Enterprise-Object? Because the only solution I found is this:
> -------
> User u = new User();
> session.defaultEditingContext().insertObject(u);
> u.setLastname(old.lastname());
> u.setFirstname(old.firstname());
> ...
> ...
> ...
> session.defaultEditingContext().saveChanges();

This is probably more suited to the wo-dev list, but...

There is a very good section in Chuck and Sacha's Practical WebObjects on implementing EO copying, which I highly recommend you read.

Basically you create a new EO, get the existing EO's attributes and relationships, iterating over them and copying from existing to new.

Attributes are easy:

	new.takeValuesFromDictionary(existing.valuesForKeys(existing.attributeKeys()));

...unless you don't want to copy all of the attributes.

Relationships are much harder, mainly because of the questions that need to be asked (i.e: do the destination objects need to be copied as well or on the relationship etc).

;david

--
David LeBer
Codeferous Software
'co-def-er-ous' adj. Literally 'code-bearing'
site: 	http://codeferous.com
blog: 	http://davidleber.net
profile:	http://www.linkedin.com/in/davidleber
twitter:	http://twitter.com/rebeld
--
WOWODC 2011 :  July 1-2-3, Montreal. http://wowodc.com