eloquent-driver icon indicating copy to clipboard operation
eloquent-driver copied to clipboard

Content API: Selecting Fields not working / error

Open helloiamlukas opened this issue 4 years ago • 3 comments

Bug Description

If you try to select just specific fields (exactly as described in the documentation), nothing happens or an error will be thrown.

How to Reproduce

Problem 1: Selecting Fields has no effect

Request

http://statamic.test/api/collections/newsletters/entries?fields=title

Response

Bildschirmfoto 2021-08-09 um 14 00 31 I would expect only the `title` attribute of each entry shown here.

Problem 2: Error when specifying specific fields

Request

http://statamic.test/api/collections/newsletters/entries?fields=id or http://statamic.test/api/collections/newsletters/entries?fields=date or http://statamic.test/api/collections/newsletters/entries?fields=uri

Response

Error: Call to a member function cascade() on null Bildschirmfoto 2021-08-09 um 14 09 34

Environment

Statamic 3.1.32 Pro Laravel 8.40.0 PHP 7.4.19 statamic/collaboration 0.1.0

Install method (choose one):

  • Fresh install from statamic/statamic

helloiamlukas avatar Aug 09 '21 12:08 helloiamlukas

This is working fine for me. Perhaps it's something unique about your setup. Would you be able to zip up your site and send it to [email protected] for us to take a quick look?

jasonvarga avatar Aug 09 '21 13:08 jasonvarga

The issue here is the eloquent driver. I'll move the issue over to that repo.

jasonvarga avatar Aug 12 '21 21:08 jasonvarga

So this works fine for me on v1, as long as one of the fields is collection... if you dont select it then you dont get the blueprint, so it throws an error when trying to augment fields.

So either we update the code to always select collection, or we just make people aware they need to?

ryanmitchell avatar Sep 07 '22 14:09 ryanmitchell

This is something we're experiencing as well. Including the "collection" field seems to be a workaround for now, but a real fix would be great!

Smef avatar Jun 29 '23 14:06 Smef

I think the solution will be to always select collection even when it's not asked for. But yep leave it with me and I'll try and come up with something before the next release.

ryanmitchell avatar Jun 29 '23 14:06 ryanmitchell

I've opened a pull request here which should solve the issue, though I need to spend some time sitting on it to decide if (a) there isnt a better way, (b) this doesnt affect other areas of the driver

ryanmitchell avatar Jun 29 '23 16:06 ryanmitchell

It turns out adding the collection field doesn't actually resolve this. The query doesn't fail, but all the fields of the entry are null when the collection field is included.

Smef avatar Jul 01 '23 02:07 Smef

@Smef are you able to supply an example repository showing the issue?

ryanmitchell avatar Jul 03 '23 16:07 ryanmitchell

@Smef did you try applying the changes made here: https://github.com/statamic/eloquent-driver/pull/183

ryanmitchell avatar Jul 07 '23 16:07 ryanmitchell

I tried updating the EntryQueryBuilder.php file with the changes in that PR. It didn't seem to make a difference with the returned data. Here's an example fields=title,price_base returns:

{
    "data": [
        {
            "title": "magnam velit accusamus et eius",
            "price_base": 37.25
        }
    ],
...

adding id and collection: fields=id,collection,title,price_base

{
    "data": [
        {
            "id": 332,
            "collection": {
                "title": "Products",
                "handle": "products"
            },
            "title": null,
            "price_base": null
        }
...

Smef avatar Jul 07 '23 22:07 Smef

@Smef works fine for me with the PR - double check you copied everything over fully:

visiting: /api/collections/pages/entries?fields=id,title,template,shares,content

{
    "data": [
        {
            "id": "4a8b8690-55c6-426d-8d08-2bc73df230ab",
            "title": "Articles",
            "template": "articles.index",
            "shares": 53,
            "content": "<p>These are my various writings. Some might say they are cool.222</p>\n"
        },
        {
            "id": "56f274ce-a723-4913-bae6-1d13833c356f",
            "title": "Home",
            "template": "home",
            "shares": 99,
            "content": "<p>I see a bad-ass mother who don't take no crap off of nobody.</p>\n"
        },
        {
            "id": "62136fa2-9e5c-4c38-a894-a2753f02f5ff",
            "title": "About",
            "template": "page",
            "shares": 54,
            "content": "<p>I'm just a kid living in the 90's, writing articles in his secret public journal wonder if someday, somewhere in the future, they will be read by someone.</p>\n"
        },
        {
            "id": "b9e4bfe3-9c12-4553-b7ef-f43c22ffaa63",
            "title": "Articles",
            "template": "articles.index",
            "shares": 21,
            "content": "<p>These are my various writings. Some might say they are cool.</p>\n"
        },
        {
            "id": "f5a53468-017e-4725-b5b6-a745df7e94cb",
            "title": "Topics",
            "template": "topics/index",
            "shares": 37,
            "content": null
        },
        {
            "id": "home",
            "title": "Home",
            "template": "home",
            "shares": 98,
            "content": "<p>I see a bad-ass mother who don't take no crap off of nobody.</p>\n"
        }
    ],

ryanmitchell avatar Jul 10 '23 06:07 ryanmitchell

Ah! I found the problem. I had accidentally updated the Statamic EntryQueryBuilder and not the eloquent-driver EntryQueryBuilder file.

Yes, this update resolved the issue, and I'm able to query the ID and slug fields without including the collection and all the other fields show up properly as well.

Thanks!

Smef avatar Jul 12 '23 15:07 Smef