Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Posting a new subresource,do I need a custom route? #72

Closed
kabasakalis opened this issue Nov 23, 2013 · 6 comments
Closed

Posting a new subresource,do I need a custom route? #72

kabasakalis opened this issue Nov 23, 2013 · 6 comments

Comments

@kabasakalis
Copy link

Following your team/players example in Subresources section of your readme file,
and assuming I have a MANY_MANY relationship,I 'd like to POST a new player in ,say team 3.Is this possible?I tried
POST /api/team/3/players/ with the new player as payload and got '405', 'Method Not Allowed' exception.After some digging in your code I think this is because a second parameter for the subresource (primary key) is expected,which makes sense if it's a GET,but not with POST obviously since it's a new record.POSTing to a subresource is not covered in this section.
Am I missing something here or should I declare a custom route to handle this ?
Thanks in advance.

@evan108108
Copy link
Owner

POST is not allowed for a subresource. You should do a PUT with the ID of
the player you would like to add to the team.

First POST the new player to the player resource. You should get back the
new player ID. Let's say that ID is 9. Then PUT that player to your
sub-resource like so: PUT /api/team/players/9 . (Note there is no payload)

That is the standard REST way to do this and should work for you.

Thanks
-Evan

On Saturday, November 23, 2013, Spiros Kabasakalis wrote:

Following your team/players example in Subresources section of your readme
file,
and assuming I have a MANY_MANY relationship,I 'd like to POST a new
player in ,say team 3.Is this possible?I tried

POST /api/team/3/players/ with the new player as payload and got '405',
'Method Not Allowed' exception.After some digging in your code I think this
is because a second parameter for the subresource (primary key) is
expected,which makes sense if it's a GET,but not with POST obviously since
it's a new record.POSTing to a subresource is not covered in this section.
Am I missing something here or should I declare a custom route to handle
this ?
Thanks in advance.


Reply to this email directly or view it on GitHubhttps://github.com//issues/72
.

@kabasakalis
Copy link
Author

I see.I understand this,but my concern is that it needs 2 roundtrips to the server.Do you think I could handle this with a custom route? Something like POST /api/team/players/{team_id}

class TeamController extends Controller{

 public function restEvents()
    {
 $this->onRest('req.post.players', function($data, $team_id) {
            //$data is the data sent in the POST
            //pseudo code
      $team=Team->findByPk($team_id);
     $newPlayer=new Player// assign attributes from $data,etc
      $team->players=array_merge($team->players,$newPlayer);
        $team->save();
            echo CJSON::encode(SUCCESS AND DATA);//pseudo code
        });
}
}

I assumed that the active-record-relation behavior that your extension uses can handle this.
Do you think this is possible?
Thanks again!

@evan108108
Copy link
Owner

Sure it's possible. It's not particularly "RESTFull" but I guess so what...
If it's a feature you really want please go ahead and build it (then submit
a pull request!). I will be happy to help you. Right now the POST action
provider throws a 405 "Method Not Allowed" if you try and POST to
sub-resource(s). You will need to create a new event and emit it here.

That said I would not get hung up on the extra round trip. These are supper
minimal and doing it this way is very clear. You create a player and then
add that player to a team. No need to try and optimize into one request at
this point. You will need to hit a pretty big scale before the extra
overhead of a second request is a real problem. In my experience premature
optimization is the root of all eval!

Thanks
-Evan

On Saturday, November 23, 2013, Spiros Kabasakalis wrote:

I see.I understand this,but my concern is that it needs 2 roundtrips to
the server.Do you think I could handle this with a custom route? Something
like POST /api/team/players/{team_id}

class TeamController extends Controller{

public function restEvents()
{
$this->onRest('req.post.players', function($data, $team_id) {
//$data is the data sent in the POST
//pseudo code
$team=Team->findByPk($team_id);
$newPlayer=new Player// assign attributes from $data,etc
$team->players=array_merge($team->players,$newPlayer);
$team->save();
echo CJSON::encode(SUCCESS AND DATA);//pseudo code
});
}
}

I assumed that the active-record-relation behavior that your extension
uses can handle this.
Do you think this is possible?
Thanks again!


Reply to this email directly or view it on GitHubhttps://github.com//issues/72#issuecomment-29143911
.

@evan108108
Copy link
Owner

O' You could handle this with a custom route (like the one you wrote above) for sure if you want to make a
one off.. You may have to change the name of the route to something other then "players" maybe "addPlayer" otherwise RESTFullYii may identify "players" as a sub-resource.

Good Luck!

On Saturday, November 23, 2013, evan frohlich wrote:

Sure it's possible. It's not particularly "RESTFull" but I guess so
what... If it's a feature you really want please go ahead and build it
(then submit a pull request!). I will be happy to help you. Right now the
POST action provider throws a 405 "Method Not Allowed" if you try and POST
to sub-resource(s). You will need to create a new event and emit it here.

That said I would not get hung up on the extra round trip. These are
supper minimal and doing it this way is very clear. You create a player and
then add that player to a team. No need to try and optimize into one
request at this point. You will need to hit a pretty big scale before the
extra overhead of a second request is a real problem. In my experience
premature optimization is the root of all eval!

Thanks
-Evan

On Saturday, November 23, 2013, Spiros Kabasakalis wrote:

I see.I understand this,but my concern is that it needs 2 roundtrips to
the server.Do you think I could handle this with a custom route? Something
like POST /api/team/players/{team_id}

class TeamController extends Controller{

public function restEvents()
{
$this->onRest('req.post.players', function($data, $team_id) {
//$data is the data sent in the POST
//pseudo code
$team=Team->findByPk($team_id);
$newPlayer=new Player// assign attributes from $data,etc
$team->players=array_merge($team->players,$newPlayer);
$team->save();
echo CJSON::encode(SUCCESS AND DATA);//pseudo code
});
}
}

I assumed that the active-record-relation behavior that your extension
uses can handle this.
Do you think this is possible?
Thanks again!


Reply to this email directly or view it on GitHubhttps://github.com//issues/72#issuecomment-29143911
.

@kabasakalis
Copy link
Author

I see.If I am too lazy,I'll take up the POST-player and PUT-player-to-team approach,sounds pretty straightforward.If I have more time,I might also try a custom route.
Thank you very much for instant feedback and congrats for you great extension!
Cheers from Athens,Greece!

@evan108108
Copy link
Owner

No problem. I was in Athens last year and loved it!

Cheers from New York City (Astoria: huge Greek community), USA.

On Saturday, November 23, 2013, Spiros Kabasakalis wrote:

Closed #72 #72.


Reply to this email directly or view it on GitHubhttps://github.com//issues/72
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants