• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Duplicating an EO and all its relationships
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Duplicating an EO and all its relationships


  • Subject: Re: Duplicating an EO and all its relationships
  • From: Jonathan Rochkind <email@hidden>
  • Date: Thu, 31 Jul 2003 17:34:41 -0500

At 12:06 PM 7/31/2003 -1000, Art Isbell wrote:

To copy an EO instance, you'll need to create a new instance, insert it into an EC as always, then for each attribute, send the new object a setter message whose argument is the value returned by sending the original object a getter message. For each relationship, send the new object an addObjectToBothSidesOfRelationshipWithKey() message the first of whose arguments is the value returned by sending the original object a getter message.

So this will likely be more than just a few lines of code.

I'm doing this now off the top of my head, but I think this has been discussed on the list before, it would be worth checking the archives. But it seems like you can do it in just a bit more than few lines of code. Surprised there isn't a built in convenience method for this, but I _think_ this'll work in general:


EOEnteprirseObject sourceObj;
EOEditingContext ec = sourceObj.editingContext();
String entityName = sourceObj.entityName();
EOEntity entity = EOUtilities.entityNamed( ec, entityName );

EOEnterpriseObject copyObj = EOUtilities.createAndInsertInstance( ec, entityName );

NSArray properties = entity.classProperties();
for ( int i = 0; i < properties.count(); i++) {
String property = (String) properties.objectAtIndex( i );
Object sourceValue = sourceObj.valueForKey( property );
//Wait, is it a relationship?
if ( entity.relationshipNamed( property ) == null) {
//it's an attribute
copyObj.takeValueForKey( sourceValue, property );
}
else {
//it's a relationship
copyObj.addObjectToBothSidesOfRelationshipWithKey( sourceValue, property );
}
}


ec.saveChanges();

Haven't tested that code. Haven't even compiled it. But maybe it'll work. I guess 20 lines of code is a bit more than a few, but not too much.

--Jonathan

Aloha,
Art
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >Re: Duplicating an EO and all its relationships (From: Art Isbell <email@hidden>)

  • Prev by Date: Re: Duplicating an EO and all its relationships
  • Next by Date: Re: Duplicating an EO and all its relationships
  • Previous by thread: Re: Duplicating an EO and all its relationships
  • Next by thread: Re: Duplicating an EO and all its relationships
  • Index(es):
    • Date
    • Thread