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

Suggestion to add multi-level relations to ActiveRecord::extraFields() #15

Open
alexweb-zz opened this issue Apr 30, 2014 · 50 comments
Open

Comments

@alexweb-zz
Copy link

Currrently there is no possibility to dynamically manage relations that should be included to respond in case when relation level is bigger than one.
For example we have model Business and you can define Locations relation like this:

    public function extraFields()
    {
        $fields = parent::fields();
        $fields[]='locations';
        return $fields;
    }

And then you can include it to respond using ?expand="locations"

But if we want to include Locations together with related location Schedules, then that doesn't work.

I suggest to implement multi-level relations (using dot notation):

    public function extraFields()
    {
        $fields = parent::fields();
        $fields[]='locations';
        $fields[]='locations.schedule';
        return $fields;
    }

And then it will be possible to use it this way: ?expand="locations, locations.schedule"

I think it would be nice, because it's very often needed to manage relation records that should be included to respond.
Is it possible?

@qiangxue
Copy link
Member

I thought about this. It's not trivial, especially if you also want to support selectively pick up certain fields from relations.

@cebe
Copy link
Member

cebe commented Apr 30, 2014

can't we delegate this to the extraFields method of the related model and use definitions from there?

@qiangxue
Copy link
Member

How will the GET query parameter looks like? How to go from there to the method parameters?

@creocoder
Copy link
Contributor

can't we delegate this to the extraFields method of the related model and use definitions from there?

We do not need do that!

Please read this: http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api#autoloading

All we need is support expand or embed parameter.

I thought about this. It's not trivial, especially if you also want to support selectively pick up certain fields from relations.

From client side there is no feature to selectively pick up certain fields from relations in expand/embed. It will be overcoding.

From model side it can be easily handled like:

class Product extends ActiveRecord
{
    public function extraFields()
    {
        return [
            'price',
            'quantity',
            'categories' => ['id', 'title'], <- LOOK HERE
        ];
    }
}

@creocoder
Copy link
Contributor

So query like GET /products?expand=price,categories will give you result:

{
    "id": 1,
    "title": "Foo",
    "price": "$10",
    "categories": [
        {
            "id": 1,
            "title": "Bar"
        },
        {
            "id": 2,
            "title": "Baz"
        }
    }
}

@xterr
Copy link

xterr commented Mar 11, 2015

+1

6 similar comments
@bogdaniy
Copy link

+1

@jcvalerio
Copy link

+1

@eberon
Copy link

eberon commented May 23, 2015

+1

@akaNightmare
Copy link

+1

@kadanin
Copy link

kadanin commented Jun 4, 2015

+1

@juanagu
Copy link

juanagu commented Jun 24, 2015

+1

@kadanin
Copy link

kadanin commented Jun 25, 2015

Where in code YII2 looks for "expand" parameter to choose which extra fields must be shown?

@cebe
Copy link
Member

cebe commented Jun 25, 2015

Where in code YII2 looks for "expand" parameter to choose which extra fields must be shown?

https://github.com/yiisoft/yii2/blob/master/framework/rest/Serializer.php#L159

@riyaskp
Copy link

riyaskp commented Aug 3, 2015

+1

@One-art
Copy link

One-art commented Aug 24, 2015

This problem make me cry, really!
I want to get list of "items" with author.
And in author I want to get Full name extra field.
But it's not working at all. Expand not using custom fields.

@jasonhancock
Copy link

+1

@ebuzzz
Copy link

ebuzzz commented Aug 31, 2015

For the moment, I've worked around this problem by overruling the fields() call in my ActiveRecord model.

class MultiLevelActiveRecord extends \yii\db\ActiveRecord
{
    public function fields()
    {
        $fields = parent::fields();

        // Add multi-level expanded fields
        $expandFields = explode(',', Yii::$app->request->getQueryParam('expand'));
        foreach ($expandFields as $field)
        {
            if (strpos($field, strtolower($this->formName()).'.') === 0)
            {
                $fields[] = substr($field, strlen($this->formName()) + 1);
            }
        }

        return $fields;
    }
}

That enabled me to use:

?expand=category,author.name

But only based on exact modal names, and not based on relations. It's dirty, but does the job for me.

@mushahidh
Copy link

+1

11 similar comments
@supr-d
Copy link

supr-d commented Sep 19, 2015

+1

@tendallas
Copy link

+1

@jacksumit
Copy link

+1

@pgarriga
Copy link

+1

@manquer
Copy link

manquer commented Nov 27, 2015

+1

@Tr9
Copy link

Tr9 commented Dec 1, 2015

+1

@Forin
Copy link

Forin commented Dec 1, 2015

+1

@archlemon
Copy link

+1

@Faryshta
Copy link
Contributor

Faryshta commented Dec 1, 2015

+1

@pedromalta
Copy link

+1

@nancoder
Copy link

+1

@alex-nesterov
Copy link

http://domain/restapi/users/1/posts/123 (#3319) +1

@dawei101
Copy link

+1

@nkostadinov
Copy link

I'm currently implementing something to handle this issue. I use scopes, so I define each scope in the model and the client requests the scopes to be returned. The request is

?scope=profile,orders,locations 

and in the model I define them as arrays like in fields(). I personally don't think that the client should be able to define exactly what to be returned it should be limited to scopes defined on the server. It can be easily used with oauth server(in my case) which limits the visible scopes for each client.

@nerburish
Copy link

+1

9 similar comments
@xfg
Copy link

xfg commented Jan 14, 2016

+1

@vadim-bulochnik
Copy link

+1

@ilves
Copy link

ilves commented Feb 18, 2016

+1

@panrus
Copy link

panrus commented Mar 29, 2016

+1

@vertamedia-teamcity-github

+1

@sazo
Copy link

sazo commented Apr 4, 2016

+1

@vnddr
Copy link

vnddr commented Apr 12, 2016

+1

@tdimdimich
Copy link

+1

@dcb9
Copy link

dcb9 commented May 2, 2016

+1

@SilverFire
Copy link
Member

Please, use 👍 emoticon to the first message in this issue instead of adding more "+1" comments. Thank you!

@Faryshta
Copy link
Contributor

Faryshta commented May 3, 2016

@SilverFire i think the reason is because reactions emojis doesn't get attention.

@SilverFire
Copy link
Member

SilverFire commented May 3, 2016

We have enough members asking for this feature, so we already know that we should take care about this issue. Clicking "+1" is OK to let us know, that you want this kind of enhancement too. We appreciate all kinds of help. We are thankful for showing, what is important for you and will take care of it. GitHub added emotions to help managing daily flow of changes in repo. Thank you

@yiisoft yiisoft locked and limited conversation to collaborators May 3, 2016
@yiisoft yiisoft unlocked this conversation May 3, 2016
@SilverFire
Copy link
Member

I'm not locking the conversation. We will be happy to hear more feedback from you

@Faryshta
Copy link
Contributor

Faryshta commented May 3, 2016

There are currently two PR's from @mdmunir with two different approaches on how to solve this feature request #11493 and #11476 I think the people here should check them and see which oneis easier to implement and use.

@gonimar
Copy link
Contributor

gonimar commented May 31, 2017

+1

1 similar comment
@lubobill1990
Copy link

+1

@samdark samdark transferred this issue from yiisoft/yii2 Nov 3, 2018
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