Re: ERRest and to many relationships
Re: ERRest and to many relationships
- Subject: Re: ERRest and to many relationships
- From: Andrew Kinnie <email@hidden>
- Date: Tue, 29 Mar 2011 16:03:54 -0400
Thanks for the tip on using create that way. I didn't know you could do that. In any event, that's the problem. NoteType is basically a lookup table. I would want to fetch an existing noteType based on some unique attribute, such as "typeName" (which ensure is unique in the EO class) I don't want to create a new noteType, I want to create the intervening object (DeviceNoteType) which has a to-one to NoteType and a to-one to Device. AND (here's the tough part - for me at least) I want to only create a new one if one doesn't already exist. So I need to fetch first, and I want to fetch based on the value passed in for the name key
So I need to fetch on the passed in value from the json, get the correct NoteType at the other end of the many to many, check if there is an existing row in the join table for the device and noteType, and create a new one, only if the intervening object doesn't exist. If it does exist, I set it's status to active and return that, otherwise, I return the new one.
That's my theory at least.
On Mar 29, 2011, at 3:47 PM, Pascal Robert wrote:
>
> Le 2011-03-29 à 15:31, Andrew Kinnie a écrit :
>
>> Well, first I tried to simply create the intervening object manually, but then I realized I wanted to know if such an object already existed, so I was going to do a fetch, but as random client wouldn't know the primary key of the NoteType, I am not sure how to do that. I could expose the primary key, but I'd much rather not.
>>
>> Presumably, the client could simple call the curl commands to create the intervening object, then add the current object with a second line, but I wanted to be able to do this in the java itself so the client could do something like:
>>
>> curl -X PUT -d "{noteType:{type:'NoteType', name:'Alert'}}" http://My-MacBook-Pro.local:9001/cgi-bin/WebObjects/MyApp.woa/ra/Device/1/addNoteType.json
>>
>> Then I would get the device, get the NoteType represented by the routeObjectForKey("noteType") and set add the noteType to the relationship.
>>
>> public WOActionResults addNoteTypeAction() {
>> Device device = Device();
>> NoteType type = routeObjectForKey("noteType");
>> // set the relationships and do whatever else
>> ...
>
> If you want to create a new NoteType to a Device, you should call the create() method in addNoteTypeAction instead of routeObjectForKey, and set the relation between the NoteType and the Device:
>
> public WOActionResults addNodeTypeAction() {
> NoteType nodeType = create(NoteType.ENTITY_NAME, yourerxkeyfilter);
> Device device = routeObjectForKey("device");
> // set the relationship and call ec.saveObjects();
> return response(nodeType, yourerxkeyfilter);
> }
>
> And your route:
>
> requestHandler.addRoute(new ERXRoute(Device.ENTITY_NAME, "/Device/{device:Device}/addNoteType", ERXRoute.Method.Put, DevicesController.class, "addNoteType"));
>
> This is untested, of course.
_______________________________________________
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