Giter Site home page Giter Site logo

Comments (6)

evan108108 avatar evan108108 commented on July 26, 2024

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
.

from restfullyii.

kabasakalis avatar kabasakalis commented on July 26, 2024

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!

from restfullyii.

evan108108 avatar evan108108 commented on July 26, 2024

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
.

from restfullyii.

evan108108 avatar evan108108 commented on July 26, 2024

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
.

from restfullyii.

kabasakalis avatar kabasakalis commented on July 26, 2024

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!

from restfullyii.

evan108108 avatar evan108108 commented on July 26, 2024

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
.

from restfullyii.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.