REST - create with relations.

I am still not sure how one should go about it with Yii default implementation of REST.

Say we have two models, Post and Category and a post can have multiple categories.

When creating a post, should we send a POST request to /posts and in the request body embed the categories, such as:




POST /posts

{"title": "the title", "body": "the body", "categories": ["category 1", "category 2"]}



Or should we send two separate requests, i.e:




POST /posts

{"title": "the title", "body": "the body"}


# this responds with {"id": 12345}



Then we send the categories




POST /posts/12345/categories

["category 1", "category 2"]



I personally think the second one makes more sense, however, Yii has no support for working like this, so i would have to basically do all the work myself to support this.

Any ideas?

Two separate requests.

@samdark - Ok, and how about Yii’s support for this out of the box (i am thinking at the getLinks() method here in models)?

It has support for it out of the box.

Hmm, no, it does not.

If you have a many to many relation type, it is impossible to get it going properly with current yii implementation, at least not in a usable way.

If i access /posts/10/categories i expect to see a nice listing of category objects, not simple objects from the junction table showing the connection in between the two. Makes sense?

You’ve asked about saving, not about listing.