Comments (6)
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 triedPOST /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.
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.
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.
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
-EvanOn 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.
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.
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:
—
Reply to this email directly or view it on GitHubhttps://github.com//issues/72
.
from restfullyii.
Related Issues (20)
- Usage without urlFormat = 'path' HOT 2
- Yii2 HOT 1
- slug for requesting resources
- LoadBalancer on Amazon Web Services causing "Unauthorized" message in Restful Yii HOT 2
- Filter is not working by case insensitive HOT 1
- Documentation HOT 1
- fig
- Error 404 Unable to resolve the request "api/series" HOT 1
- Many to many Relationship HOT 3
- Can i login by AJAX? HOT 4
- PHP Error on processing nullable relation data (PUT, POST)
- How to pass params
- Multiple model save with relation HAS_MANY HOT 5
- support for yii 2.0+? HOT 1
- del
- Error is thrown when overriding model attributes with request's parameters HOT 5
- Sub-Resources on HAS_ONE or HAS_MANY not working HOT 2
- feature request: ability to pass callable to onRest HOT 1
- API requests rate limit HOT 2
- Filter model in a custom request HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from restfullyii.