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

How to get relationship data for REST? #3319

Closed
jungyuyu opened this issue May 2, 2014 · 4 comments
Closed

How to get relationship data for REST? #3319

jungyuyu opened this issue May 2, 2014 · 4 comments

Comments

@jungyuyu
Copy link

jungyuyu commented May 2, 2014

I need GET the user post list, request url as follow:
http://domain/restapi/users/1/posts

I add getPosts() in /models/User.php
public function getPosts()
{
return $this->hasMany(Post::className(), ['author_ID' => 'ID']);
}

But does not work, need modify urlManager? or extend some method()?
how to do it? guide me, please!

My tables as follow:
CREATE TABLE yii2_user (
ID bigint(20) unsigned NOT NULL AUTO_INCREMENT,
...
)

CREATE TABLE yii2_post (
ID bigint(20) unsigned NOT NULL AUTO_INCREMENT,
author_ID bigint(20) unsigned NOT NULL DEFAULT '0',
...
)

Best regards,

@Ragazzo
Copy link
Contributor

Ragazzo commented May 2, 2014

you can use expand parameter, see this info in rest guide. However getting particular one info about related model is not supported now, so this issue can be open i think for further discussion @qiangxue ?

@jungyuyu
Copy link
Author

jungyuyu commented May 2, 2014

Thanks Ragazzo,

I modified /models/User.php to add extraFields() etc as follow:

public function extraFields()
{
return ['posts'];
}

and request GET http://domain/restapi/users/1?expand=posts

It's work fine, but not enough to support multilevel data for REST, e.g.
Just GET one http://domain/restapi/users/1/posts/123

Because i use AngularJS to design client, and AngularJS have full REST feature:
https://github.com/mgonto/restangular

If Yii2 can full support, it's really great!

@Ragazzo
Copy link
Contributor

Ragazzo commented May 2, 2014

@jungyuyu well, it will have support once somone from core or other developers will have time for that ) you also can submit PR if ;) Lets keep this issue open for this feature discussion.

@cebe
Copy link
Member

cebe commented May 3, 2014

duplicate of #3303

@cebe cebe closed this as completed May 3, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants