Skip to content

Best practice to include a COUNT query in collection operation #1083

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

Closed
superbull opened this issue Apr 1, 2019 · 4 comments
Closed

Best practice to include a COUNT query in collection operation #1083

superbull opened this issue Apr 1, 2019 · 4 comments

Comments

@superbull
Copy link

In case I have below Entities:

class Book
{
    private $id;
    private $title;
    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Review", mappedBy="book")
     */
    private $reviews;
}

class Review
{
    private $id;
    private $body;
    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Book", inversedBy="reviews")
     */
    private $book;
}

When I retrieve a list of Books, I want also the total number of the reviews a book contains. The response should like below:

{
  "hydra:member": [
    {
      "@id": "/books/e0820d66-81cd-4e48-8b09-f2931a39951b",
      "@type": "http://schema.org/Book",
      "title": "345",
      "reviewsCount": 20
    }
  ]
}

What is the right way to implement this in API Platform?

@teohhanhui
Copy link
Contributor

Ideally, it should be something like:

{
  "hydra:member": [
    {
      "@id": "/books/e0820d66-81cd-4e48-8b09-f2931a39951b",
      "@type": "http://schema.org/Book",
      "title": "345",
      "reviews": {
        "@id": "/books/e0820d66-81cd-4e48-8b09-f2931a39951b/reviews",
        "@type": "hydra:Collection",
        "hydra:totalItems": 20
      }
    }
  ]
}

@superbull
Copy link
Author

Yes, it looks better.
And then I found this might be related to the issue api-platform/core#1686

@vincentchalamon
Copy link
Contributor

vincentchalamon commented Apr 7, 2019

Closing as a solution has been found (cf. previous comment)

@silviodeligios
Copy link

Ideally, it should be something like:

{
  "hydra:member": [
    {
      "@id": "/books/e0820d66-81cd-4e48-8b09-f2931a39951b",
      "@type": "http://schema.org/Book",
      "title": "345",
      "reviews": {
        "@id": "/books/e0820d66-81cd-4e48-8b09-f2931a39951b/reviews",
        "@type": "hydra:Collection",
        "hydra:totalItems": 20
      }
    }
  ]
}

hi, i'm trying to achieve this but i can't figure out how to do it. Is this possible? Where can i found some documentation about it?

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

4 participants