Code Monkey home page Code Monkey logo

Comments (50)

Faryshta avatar Faryshta commented on June 2, 2024 19

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.

from active-record.

creocoder avatar creocoder commented on June 2, 2024 6

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
        ];
    }
}

from active-record.

ebuzzz avatar ebuzzz commented on June 2, 2024 4

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.

from active-record.

SilverFire avatar SilverFire commented on June 2, 2024 1

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

from active-record.

qiangxue avatar qiangxue commented on June 2, 2024

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

from active-record.

cebe avatar cebe commented on June 2, 2024

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

from active-record.

qiangxue avatar qiangxue commented on June 2, 2024

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

from active-record.

creocoder avatar creocoder commented on June 2, 2024

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"
        }
    }
}

from active-record.

xterr avatar xterr commented on June 2, 2024

+1

from active-record.

bogdaniy avatar bogdaniy commented on June 2, 2024

+1

from active-record.

jcvalerio avatar jcvalerio commented on June 2, 2024

+1

from active-record.

eberon avatar eberon commented on June 2, 2024

+1

from active-record.

akaNightmare avatar akaNightmare commented on June 2, 2024

+1

from active-record.

kadanin avatar kadanin commented on June 2, 2024

+1

from active-record.

juanagu avatar juanagu commented on June 2, 2024

+1

from active-record.

kadanin avatar kadanin commented on June 2, 2024

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

from active-record.

cebe avatar cebe commented on June 2, 2024

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

from active-record.

riyaskp avatar riyaskp commented on June 2, 2024

+1

from active-record.

One-art avatar One-art commented on June 2, 2024

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.

from active-record.

jasonhancock avatar jasonhancock commented on June 2, 2024

+1

from active-record.

mushahidh avatar mushahidh commented on June 2, 2024

+1

from active-record.

supr-d avatar supr-d commented on June 2, 2024

+1

from active-record.

tendallas avatar tendallas commented on June 2, 2024

+1

from active-record.

jacksumit avatar jacksumit commented on June 2, 2024

+1

from active-record.

pgarriga avatar pgarriga commented on June 2, 2024

+1

from active-record.

manquer avatar manquer commented on June 2, 2024

+1

from active-record.

Tr9 avatar Tr9 commented on June 2, 2024

+1

from active-record.

Forin avatar Forin commented on June 2, 2024

+1

from active-record.

archlemon avatar archlemon commented on June 2, 2024

+1

from active-record.

Faryshta avatar Faryshta commented on June 2, 2024

+1

from active-record.

pedromalta avatar pedromalta commented on June 2, 2024

+1

from active-record.

nancoder avatar nancoder commented on June 2, 2024

+1

from active-record.

alex-nesterov avatar alex-nesterov commented on June 2, 2024

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

from active-record.

dawei101 avatar dawei101 commented on June 2, 2024

+1

from active-record.

nkostadinov avatar nkostadinov commented on June 2, 2024

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.

from active-record.

nerburish avatar nerburish commented on June 2, 2024

+1

from active-record.

xfg avatar xfg commented on June 2, 2024

+1

from active-record.

vadim-bulochnik avatar vadim-bulochnik commented on June 2, 2024

+1

from active-record.

ilves avatar ilves commented on June 2, 2024

+1

from active-record.

panrus avatar panrus commented on June 2, 2024

+1

from active-record.

vertamedia-teamcity-github avatar vertamedia-teamcity-github commented on June 2, 2024

+1

from active-record.

sazo avatar sazo commented on June 2, 2024

+1

from active-record.

vnddr avatar vnddr commented on June 2, 2024

+1

from active-record.

tdimdimich avatar tdimdimich commented on June 2, 2024

+1

from active-record.

dcb9 avatar dcb9 commented on June 2, 2024

+1

from active-record.

Faryshta avatar Faryshta commented on June 2, 2024

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

from active-record.

SilverFire avatar SilverFire commented on June 2, 2024

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

from active-record.

SilverFire avatar SilverFire commented on June 2, 2024

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

from active-record.

gonimar avatar gonimar commented on June 2, 2024

+1

from active-record.

lubobill1990 avatar lubobill1990 commented on June 2, 2024

+1

from active-record.

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.