Re: Many-to-Many with visible attributes
Re: Many-to-Many with visible attributes
- Subject: Re: Many-to-Many with visible attributes
- From: Cornelius Jaeger <email@hidden>
- Date: Mon, 19 Jan 2004 14:24:50 +0100
hi,
you can't use a flattened relationship. you need to use the normal m
<->> n <<-> m relationships, and you have to write your own methods to
ensure data integrity.
also beware of the delete rules in eomodeler.
your setup should be something like this:
Sale can have many ServiceSale's and owns it's ServiceSale objects and
propagate's it's primary key.
ditto for Service, it own's ServiceSale's and propagate's it's primary
keys.
ServiceSale simply nullifies it's relations on delete.
here are some methods that you should implement in the Sale and Service
object respectively. depending on which object creates which
relationship:
the example here is in the Sale object.
public ServiceSale serviceSale(Service aService) {
ServiceSale result = serviceSaleIfAny(folder);
if(result == null) {
result = new ServiceSale();
editingContext().insertObject(result);
aService.addObjectToBothSidesOfRelationshipWithKey(result,
"serviceSales");
addObjectToBothSidesOfRelationshipWithKey(result,
"serviceSales");
} return result;
}
// this is a convenience method, priority is a Number attribute in
your ServiceSale entity i use this for sorting for example
public void addService(Service aService, int priority) {
serviceSale(aService).setIntPriority(priority);
}
public void removeService(Service aService) {
ServiceSale serviceSale = serviceSaleIfAny(aService);
removeObjectFromBothSidesOfRelationshipWithKey(aService,
"serviceSales");
}
public ServiceSale serviceSaleIfAny(Service aService) {
EOQualifier qualifier = new EOKeyValueQualifier("service",
EOQualifier.QualifierOperatorEqual, aService);
NSArray result =
EOQualifier.filteredArrayWithQualifier(services(), qualifier);
return (ServiceSale) result.lastObject(result);
}
public NSArray services() {
return (NSArray) serviceSales().valueForKey("service");
}
public void addServices(NSArray array, String str) {
int priority = 0;
for (Enumeration e = array.objectEnumerator();
e.hasMoreElements();) {
Service aService = (Service)e.nextElement();
addService(aService, priority++);
}
}
your ServiceSale should implement the following methods. eh, it doesn't
have to, this has just been working for me...
public class ServiceSale {
public ServiceSale() {
super();
setIntPriority(0);
}
public int intPriority() {
if(priority() != null)
return priority().intValue();
else
return 0;
}
public void setIntPriority(int value) { setPriority(new
Integer(value)); }
}
hth
cornelius
On Jan 16, 2004, at 1:09 AM, Ricardo Strausz wrote:
> Did you try addToBothSidesOfRelation method?
>
> On Jan 14, 2004, at 11:45, Jaime Magiera wrote:
>
>> What is the proper way to handle a many-to-many relationship with
>> visible attributes? (this is in JavaClient) Heres' what I'm working
>> on...
>>
>> Sale <- ServiceSale -> Service
>>
>> -------------------------
>>
>>
>> mysql> describe ServiceSales;
>>
>> +---------------+---------------+------+-----+---------+-------+
>> | Field | Type | Null | Key | Default | Extra |
>> +---------------+---------------+------+-----+---------+-------+
>> | endTime | timestamp(14) | YES | | NULL | |
>> | comments | text | YES | | NULL | |
>> | serviceID | int(11) | | PRI | 0 | |
>> | startTime | timestamp(14) | YES | | NULL | |
>> | transactionID | int(11) | | PRI | 0 | |
>> +---------------+---------------+------+-----+---------+-------+
>>
>> If I flatten the relationship and just use "Services" in the Sale
>> object, D2JC jumps directly to selecting the Service, which doesn't
>> allow me to set the other data. If I try making ServiceSale visible
>> to the Sale, the ServiceSale doesn't get the Sale primary key passed
>> to when it being added. The Propagate Primary Key box IS checked.
>> Trying to save a ServiceSale to a Sale throws and error that a Sale
>> hasn't been set.
>>
>> (is this making sense? :)
>>
>> Many-to-Many is still one of those areas that's new to me, thanks for
>> any help.
>>
>> Jaime
>>
>> _______________________________________________
>> WebObjects-dev mailing list
>> email@hidden
>> http://www.omnigroup.com/mailman/listinfo/webobjects-dev
>
> _______________________________________________
> WebObjects-dev mailing list
> email@hidden
> http://www.omnigroup.com/mailman/listinfo/webobjects-dev
_______________________________________________
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.