question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom Collection "GET" Method - API Platform

See original GitHub issue

Hi everyone,

I have an entity that i can get with the standard get operation (collection / or id one) :

/**
 * Insee
 *
 * @ApiResource(
 *     collectionOperations={"get"},
 *     itemOperations={"get"},
 *     attributes={
 *     }
 * )
 * @ApiFilter(SearchFilter::class, properties={"insCode": "exact", "insLibelle": "partial"})
 * @ORM\Table(name="dif_zonages.insee", indexes={@ORM\Index(name="FK_INSEE_CANTON", columns={"CAN_CODE"})})
 * @ORM\Entity
 */
class Insee
{

    /**
     * @var string
     *
     * @ORM\Column(name="INS_CODE", type="string", length=5, nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $insCode = '';

    /**
     * @var string|null
     *
     * @ORM\Column(name="INS_LIBELLE", type="string", length=254, nullable=true)
     */
    private $insLibelle;

    /**
     * @var float|null
     *
     * @ORM\Column(name="INS_LATITUDE", type="float", precision=10, scale=0, nullable=true, options={"comment"="Latitude de la commune (coordonnées géographiques)"})
     */
    private $insLatitude;

    /**
     * @var float|null
     *
     * @ORM\Column(name="INS_LONGITUDE", type="float", precision=10, scale=0, nullable=true, options={"comment"="Longitude de la commune (coordonnées géographiques)"})
     */
    private $insLongitude;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->insdCodeSource = new \Doctrine\Common\Collections\ArrayCollection();
    }

Etc .....

    /**
     * @return float|null
     */
    public function getInsLatitude(): ?float
    {
        return $this->insLatitude;
    }

    /**
     * @param float|null $insLatitude
     */
    public function setInsLatitude(?float $insLatitude): void
    {
        $this->insLatitude = $insLatitude;
    }

    /**
     * @return float|null
     */
    public function getInsLongitude(): ?float
    {
        return $this->insLongitude;
    }

    /**
     * @param float|null $insLongitude
     */
    public function setInsLongitude(?float $insLongitude): void
    {
        $this->insLongitude = $insLongitude;
    }

}

However, i want to add a custom "GET"opération so i can get a list of Insee object from 2 parameters : latitude and longitude. Can you help me what should i use? I saw that we can create Extension but the doc is very limited so…

Thanks in advance !

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

13reactions
yivicommented, Apr 9, 2019

Would be nice to add an example of this in the documentation. Something that shows how a minimal implementation would loook like.

What this action should return (we know that on item_operation GET we can return either an entity or a Response, what should we return for a collection GET? A Doctrine collection? An array|\Traversable?

2reactions
simocheccolicommented, Jun 11, 2019

Using only your DataProvider and your Entity with correct annotations should be enough. I use this without creating any special controller for this entity.

@foxsdev Any usefull example? I’have already try by tagging DataProvider (custom-collection-data-provider) but it’s doesn’t work as expected

Read more comments on GitHub >

github_iconTop Results From Across the Web

API Platform - How to create a custom collection operation ...
I'm using Symfony 5 and Doctrine, I exposed entities with annotation @Api Platform and I see CRUD operations on the API doc. But...
Read more >
Creating Custom Operations and Controllers - API Platform
This custom operation behaves exactly like the built-in operation: it returns a JSON-LD document corresponding to the id passed in the URL.
Read more >
Custom Resource GET Item > API Platform Part 3
Our collection operation returns some hardcoded DailyStats . Let's pretend that we have a JSON file filled with this data... or maybe in...
Read more >
How to use custom DTO's in API Platform? ⋆ Developer Wouter
API Platform then creates the basic CRUD (create, read, update, delete) endpoints, such as GET, POST, PUT and DELETE. This is fine for...
Read more >
Healthcheck - Custom Endpoint [API Platform]
Hard To Port. With the DB reset process disabled for the time being, let's see where we now get with our test: vendor/bin/behat ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found