'Api platform filtration group by issue

I'm using:

  • api platform 2.6.5
  • symfony 5.3.6
  • php 8.0.3

I trying to do custom filtration by subresource quantity. Problem what I encountered when I group resource it wrong display subresources (just 1 not all).

Result without any filtration:

"@id": "/api/formats/7",
      "@type": "Format",
      "title": "Cuda urody",
      "categories": [],
      "createdAt": "2021-12-02T12:53:18+01:00",
      "updatedAt": "2021-12-02T12:53:18+01:00",
      "seasons": [
        {
          "@id": "/api/seasons/7",
          "@type": "Season",
          "position": 0,
          "seasonNumber": "1"
        },
        {
          "@id": "/api/seasons/12",
          "@type": "Season",
          "position": 0,
          "seasonNumber": "2"
        }
      ]
...

Used filtration:

$alias = $queryBuilder->getRootAliases()[0];
$queryBuilder->groupBy($alias);

Result with filtration (missing subresource):

"@id": "/api/formats/7",
      "@type": "Format",
      "title": "Cuda urody",
      "categories": [],
      "createdAt": "2021-12-02T12:53:18+01:00",
      "updatedAt": "2021-12-02T12:53:18+01:00",
      "seasons": [
        {
          "@id": "/api/seasons/7",
          "@type": "Season",
          "position": 0,
          "seasonNumber": "1"
        }
      ]
...

Expectation: after using filter with grouping - get full subresources like in 1st example.

Yes I need use group in query to display filter results. Am I missing something or it's just api platform issue?

Probably problem is the way query is generated in api platform. I didn't notice problem when grouping Entity in Repository.

EDIT: I've managed to deal with this by adding on subresource (seasons)

fetch="EXTRA_LAZY"

so now it looks like

/**
 * @ORM\OneToMany(targetEntity=Season::class, mappedBy="format", fetch="EXTRA_LAZY")
 * @Groups({"format:read", "format:write"})
 * @Assert\Valid
 */
#[ApiSubresource]
private $seasons;

I'm not sure if it is the proper way, but it works for me.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source