Re: Newbie vs Many-To-Many (0 win-1 win)
Re: Newbie vs Many-To-Many (0 win-1 win)
- Subject: Re: Newbie vs Many-To-Many (0 win-1 win)
- From: Riccardo De Menna <email@hidden>
- Date: Sun, 2 Mar 2003 17:33:00 +0100
A ha... I should change the subject to 1 win - 1 win
I've done my homework... after studying carefully what you told me and
the documentation on EORelationshipManager I came to the conclusion
that what I'm trying to do is not exactly what
addObjectToBothSidesOfRelationshipWithKey() and the others are meant
for.
The problem is that my Contact profile also allows to reassign the
Contact to a different Client. The user is not bound to the original
relationship and thus, only adding or removing the object is not
sufficient since the original owner has to be notified as well.
I mean that I need to remove the relationship from the old Client and
add it to the new one. I've used the
EOEditingContext.currentEventSnapshotForObject which works great.
Obviously you have to call this stuff before updating the
editingContext.
Step 1: get the snapshot with all the old values of the record that
needs updating
Step 2: determine changed keys and intersect with relationship keys
Step 3: cycle trough all to-one relationships by removing the record
from the old relationship and adding to the new one
Step 4: cycle trough all the to-many relationship by removing the
record from all the previous owners and adding to the new ones
I've named the method
updateObjectForBothSidesOfRelationshipWithKey(EOGenericRecord
objectToUpdate, String relationshipToUpdate) since it basically
performs exactly the same thing that the add and remove do but with the
addition that it cleans old relationships.
It works great but I still have the feeling that it could have been
done with some prewritten thing. I'm sure that I'll find out that there
was a simple way of doing it without writing all this. I wrote my first
line in Java just 4 days ago and I'm still not very proficient with the
documentation.
BTW, thanks Art, I'm really getting into this stuff and (coming from a
procedural scripting background) I'm shocked by the simplicity and
power of WO.
I've included the method here. Possible improvements might be to
determine the relationshipToUpdate parameter automatically and avoid
removing all the relationships before adding (in the case that one has
not changed).
Cheers,
Ric
public void
updateObjectForBothSidesOfRelationshipWithKey(EOGenericRecord
objectToUpdate, String relationshipToUpdate) {
NSDictionary snapshot =
objectToUpdate.editingContext().currentEventSnapshotForObject(objectToUp
date);
NSDictionary changes =
objectToUpdate.changesFromSnapshot(snapshot);
NSArray changedKeys = changes.allKeys();
NSArray toOneKeys = objectToUpdate.toOneRelationshipKeys();
NSArray toManyKeys = objectToUpdate.toManyRelationshipKeys();
java.util.Enumeration keyEnumerator =
changedKeys.objectEnumerator();
while (keyEnumerator.hasMoreElements()) {
String aKey = (String)keyEnumerator.nextElement();
if ( toOneKeys.containsObject(aKey) ) {
// Get the objects that need to be updated
EOGenericRecord oldObject =
(EOGenericRecord)snapshot.objectForKey(aKey);
EOGenericRecord newObject =
(EOGenericRecord)objectToUpdate.valueForKey(aKey);
// Invoke removeObjectFromPropertyWithKey and
addObjectToBothSidesOfRelationshipWithKey on the objects
oldObject.removeObjectFromPropertyWithKey(objectToUpdate,relationshipToU
pdate);
newObject.addObjectToBothSidesOfRelationshipWithKey(objectToUpdate,relat
ionshipToUpdate);
} else if ( toManyKeys.containsObject(aKey) ) {
// Get arrays of objects that need to be updated
NSArray oldObjects =
(NSArray)snapshot.objectForKey(aKey);
NSArray newObjects =
(NSArray)objectToUpdate.valueForKey(aKey);
// Enumerate performing removeObjectFromPropertyWithKey
and addObjectToBothSidesOfRelationshipWithKey
java.util.Enumeration oldObjEnumerator =
oldObjects.objectEnumerator();
while (oldObjEnumerator.hasMoreElements()) {
EOGenericRecord oldObject =
(EOGenericRecord)oldObjEnumerator.nextElement();
oldObject.removeObjectFromPropertyWithKey(objectToUpdate,relationshipToU
pdate);
}
java.util.Enumeration newObjEnumerator =
newObjects.objectEnumerator();
while (newObjEnumerator.hasMoreElements()) {
EOGenericRecord newObject =
(EOGenericRecord)newObjEnumerator.nextElement();
newObject.addObjectToBothSidesOfRelationshipWithKey(objectToUpdate,relat
ionshipToUpdate);
}
}
}
}
> On Friday, February 28, 2003, at 02:45 PM, Riccardo De Menna wrote:
>
>> I have to entities, Clients and Contacts. Contacts has a nice
>> "Profile" component with a WOBrowser that lists the Clients to which
>> you can connect the record. The submission of this page immediately
>> saves the editing context. It work's fine and data is correctly
>> added to db's but, when I switch to check the "Client" profile (where
>> I should see the newly added Contact), the record is not there.
>> If I quit and restart the application though, the changes are
>> reflected and everything is ok. I assume the problem has to do with
>> the fact that I've missed the part where I also update the
>> Client.contacts array in memory (even though the db's are already
>> populated with correct data). But I'm confused on what should I do.
>> Should I use the addObjectToPropertyWithKey or
>> addObjectToBothSidesOfRelationshipWithKey when saving the editing
>> context? Even if I'm just updating a record and not adding? Or should
>> I simply refetch everything so that the new configuration is loaded?
>
> You should always use addObjectToBothSidesOfRelationshipWithKey()
> which works regardless of whether the relationship is to-one, to-many,
> flattened, or has no inverse relationship (i.e., no "other side").
> addObjectToBothSidesOfRelationshipWithKey() invokes
> addObjectToPropertyWithKey() for the relationship whose key is
> specified and its inverse relationship, if it exists.
>
> If you used addObjectToPropertyWithKey(), I think you've analyzed the
> results correctly; the inverse relationship, Client.contacts, wasn't
> updated in memory when you updated Contact.clients.
>
> Art
> http://homepage.mac.com/aisbell/
> _______________________________________________
> 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.