Re: ERRest and to many relationships
Re: ERRest and to many relationships
- Subject: Re: ERRest and to many relationships
- From: Pascal Robert <email@hidden>
- Date: Tue, 29 Mar 2011 15:47:51 -0400
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.
>
> However, the routeObjectForKey gets a null value, presumably because there is no key "noteType" in the Device class. I gather there may be some way to do this with variable substitution, but again, if I don't know the pk of the NoteType, I'm confused about how I should be doing this.
>
> Andrew
>
> On Mar 29, 2011, at 2:59 PM, Pascal Robert wrote:
>
>>
>> Le 2011-03-29 à 14:52, Andrew Kinnie a écrit :
>>
>>> Hi again.
>>>
>>> I have been able to get my ERRest app to create new objects, and with a to-one relationship based on the example app. I note that apparently ERRest (maybe rest in general) does not allow you to add an object to a to-many relationship directly. Apparently you need to first GET the intervening object, or create one if it doesn't exist, then add the object to it from the other side. e.g. with Organization ->> Member, you'd have to get or create the organization, then add the member to it.
>>
>> I had this "problem" too, but Mike said it should work, and from memory it worked for a test case I did, but it didn't work in a specific project and the problem seemed to be because of non-model attributes that I had in this entity. Sadly, I didn't find the source of the problem before I left my job.
>>
>>> What I want to do is create an Action method that allows me to look for an existing intervening object based on an attribute, and if one doesn't exist, then create one. Then set that intervening object's relationships back to original object and the pointed to object.
>>>
>>> So:
>>>
>>> I have an entity: Device which has a to-many to another entity NoteType (which in turn has a to-many back to Device)
>>> There is an intervening entity DeviceNoteType which has a to-one to Device and another to NoteType
>>>
>>> So in my DeviceController, I want to have an Action method updateNoteTypes and perhaps another addToNoteTypes
>>>
>>> I see in the wiki that this needs to be done in the two steps mentioned above, but I don't want to require the calling app to do that, but rather do it myself (e.g. passing in json with a name:"myNoteType") and be able to have my method do the necessary fetching and setting.
>>>
>>> Whenever I try to do this, I get null as the value from routeObjectForKey. Actually, I was getting that anyway, until I called the entity method in the controller (e.g. the device() method in the DeviceController class, then I can access the keys from that device. But that doesn't help here. I looked at the example app and do see any to-manys in there, and I've gone through the screencast from WO-NoVA multiple times. I'm just not seeing it.
>>
>> Can you post the methods you are using?
>
_______________________________________________
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