Problems with addObjectToBothSidesOfRelationshipWithKey
Problems with addObjectToBothSidesOfRelationshipWithKey
- Subject: Problems with addObjectToBothSidesOfRelationshipWithKey
- From: Owen McKerrow <email@hidden>
- Date: Fri, 19 Oct 2007 11:24:04 +1000
Hi All,
Im having a problem with addObjectToBothSidesOfRelationshipWithKey,
either that or I don't understand how its meant to work correctly.
That is when you use addObjectToBothSidesOfRelationshipWithKey the
relationships between both objects should be updated. So if you have
a relationship between a dog and a cat, when you use
addObjectToBothSidesOfRelationshipWithKey to set the dog's
relationship to cat, its should auto set the cat's relationship back
to the dog.
Im writing some code for my Person class, which when given another
person "merges" them into itself. That is sets all of the given
persons relationships to the first person.
Now Person has a toMany relationship with AuthorPubs and AuthorPubs
has a toOne relationship back to Person. Set up like this inside
EOModler :
Table : Person
Relationship : publicationsAuthorOf, toMany, optional
Destination : AuthorPubs
Source Att : rowID
Dest Att : personID
Table : AuthorPubs
Relationship : person, toOne, mandetory
Destination : Person
Source Att : personID
Dest Att : rowID
Now here's the code inside the Person class that does the "merging" :
public void mergePersonIntoMe(Person person, boolean delete) throws
Exception
{
.....
swaptoMe(person.publicationsAuthorOf(),inverseForRelationshipKey
("publicationsAuthorOf"));
....
}
/**
Given an array of objects and a relationship name, change all the
objects so that they belong to this person
*/
public void swaptoMe(NSArray theItems, String relationshipName)
throws Exception
{
int i;
log.debug("Reverse relationship is : " + relationshipName);
if (theItems != null ) {
for (i=0;i<theItems.count();i++) {
try {
OMGenericRecord tempRec = (OMGenericRecord)
theItems.objectAtIndex(i);
tempRec.addObjectToBothSidesOfRelationshipWithKey
(this,relationshipName);
//Check if we are copying across author/editors, if so we nee to
update their name
if (tempRec instanceof AuthorPubs ) {
log.debug("Person for publication : " + tempRec.valueForKey
(relationshipName));
if (publicationsAuthorOf().containsObject(tempRec) ) {
log.debug("Person is author.");
} else if (publicationsEditorOf().containsObject(tempRec)){
log.debug("Person is editor.");
} else {
log.debug("Person is not associated with the publication");
}
tempRec.takeValueForKey(firstNameToUseForPublications
(),"firstName");
tempRec.takeValueForKey(lastNameToUseForPublications
(),"lastName");
tempRec.takeValueForKey(middleNameToUseForPublications
(),"middleName");
}
} catch ( Exception ex) {
throw ex;
}
}
}
}
OK so I go to merge the person S Anderson into Sharon Anderson.
Now heres the output from those debug statements :
11:18:40,038 DEBUG [WorkerThread3] (Person:2284 swaptoMe) - Reverse
relationship is : person
11:18:40,039 DEBUG [WorkerThread3] (Person:2290 swaptoMe) - Person
for publication before swap : External:(Active_Name=S Anderson)
{rowID=11477;}
11:18:40,046 DEBUG [WorkerThread3] (Person:2295 swaptoMe) - Person
for publication after swap : Researcher:(Active_Name=Sharon Anderson,
Staff_Number=xxx){rowID=10000;}
11:18:40,047 DEBUG [WorkerThread3] (Person:2301 swaptoMe) - Person is
not associated with the publication
11:18:40,049 DEBUG [WorkerThread3] (Person:2290 swaptoMe) - Person
for publication before swap : External:(Active_Name=S Anderson)
{rowID=11477;}
11:18:40,050 DEBUG [WorkerThread3] (Person:2295 swaptoMe) - Person
for publication after swap : Researcher:(Active_Name=Sharon Anderson,
Staff_Number=xxx){rowID=10000;}
11:18:40,050 DEBUG [WorkerThread3] (Person:2301 swaptoMe) - Person is
not associated with the publication
11:18:40,051 DEBUG [WorkerThread3] (Person:2290 swaptoMe) - Person
for publication before swap : External:(Active_Name=S Anderson)
{rowID=11477;}
11:18:40,053 DEBUG [WorkerThread3] (Person:2295 swaptoMe) - Person
for publication after swap : Researcher:(Active_Name=Sharon Anderson,
Staff_Number=xxx){rowID=10000;}
11:18:40,054 DEBUG [WorkerThread3] (Person:2301 swaptoMe) - Person is
not associated with the publication
As you can see the AuthorPubs person is changed to the new person.
However this person has not had the AuthorPub added back to them.
The changes are made in the database and if I log out and log back in
its all nice and happy.
Can someone see something dumb Im doing ? Am I not understanding the
behavior of addObjectToBothSidesOfRelationshipWithKey correctly ?
Owen McKerrow
WebMaster, emlab
Ph : +61 02 4221 5517
http://emlab.uow.edu.au
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - -
"I like the way this project has somehow, against all common sense,
got itself made."
- Peter Jackson, "The Lord of The Rings"
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden